Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98336
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Amit Ramon <amit.ramon@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | argparse: use of double dash to separate options and positional arguments |
| Date | Fri, 6 Nov 2015 12:07:43 +0200 |
| Lines | 59 |
| Message-ID | <mailman.71.1446804597.16136.python-list@python.org> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii; format=flowed |
| X-Trace | news.uni-berlin.de /wJHV9fy/hnpjQBHhG1hkwmN10v2kdeNzBhr+uCnEmjA== |
| Return-Path | <amit.ramon@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'unrecognized': 0.07; 'argument,': 0.09; 'inserted': 0.09; 'positional': 0.09; 'wrong,': 0.09; '(but': 0.15; 'argument': 0.15; 'subject: \n ': 0.15; 'argparse': 0.16; 'arguments:': 0.16; 'posix': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:argparse': 0.16; 'subject:options': 0.16; 'string': 0.17; 'documented': 0.18; "shouldn't": 0.18; '(in': 0.18; 'arguments': 0.22; 'bug?': 0.22; 'parser': 0.22; 'trying': 0.22; 'seems': 0.23; 'thanks,': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'error': 0.27; 'this.': 0.28; 'exists,': 0.29; 'invoke': 0.29; 'quiet': 0.29; 'starts': 0.29; "i'm": 0.30; 'print': 0.30; 'code': 0.30; 'option': 0.31; 'options': 0.33; 'common': 0.33; 'list': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'subject:use': 0.35; 'received:74.125.82': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'charset:us-ascii': 0.37; 'doing': 0.38; 'itself': 0.38; 'anything': 0.38; 'skip:p 20': 0.38; 'someone': 0.38; 'end': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'hello,': 0.40; 'some': 0.40; 'questions': 0.40; 'easy': 0.60; 'hope': 0.61; 'world': 0.64; 'skip:a 40': 0.64; 'here': 0.66; 'received:185': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:mime-version:content-type :content-disposition:user-agent; bh=uhOZvoQBoUGWof8JMV8maZvhBgOmn9C/Lk4DlIe5EUk=; b=AZkuGUYDU6zZI7F12yWyEfVOsC5XakNYlPoT9y25X+PGfht3fC+XRzN97W7GjOwOIs lZhnWXeMxzQ2f5G4cjmoCR6Y5MjnQtorWXiYXpwiJIWece6hoIyK9lMoMZw6Np4HTD+Z kOWm7IHapEi7ALHNhtiB1FHgJ8jK3OShvYSXkmIilVlRnB+4sp+rVNBZUxi6tuGVfKlt WFd/G45exD+kdY2ieLGnRisZT/QczIBAFx664szw+7Wq0xh2TT/pTy8kx/jB8NtGix2A Trwx9CvlrGUtlIsZPxhXbCrzYwRQfbuklDCnlqWyczk56DgW/2OrhqNTR+wkveq23Axs /f+Q== |
| X-Received | by 10.28.13.75 with SMTP id 72mr8716712wmn.20.1446804466425; Fri, 06 Nov 2015 02:07:46 -0800 (PST) |
| Content-Disposition | inline |
| User-Agent | Mutt/1.5.24 (2015-08-30) |
| X-Mailman-Approved-At | Fri, 06 Nov 2015 05:09:56 -0500 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:98336 |
Show key headers only | View raw
Hello,
I'm trying to use argparse in a program that gets some options and
positional arguments. I want to collect all the positional arguments
in a single argument using the REMAINDER option to add_argument() as
shown bellow:
parser = argparse.ArgumentParser(description='Test argparse')
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('cmd_args', nargs=argparse.REMAINDER)
print parser.parse_args()
This works well unless the first positional argument starts with a
'-'.
For example, with the above code
my_prog -v hello world
works well, but
my_prog -v -x hello world
Fails with an error message 'error: unrecognized arguments: -x'.
If I invoke the program with a '--' at the end of the options (as I
understand is a common standard and a POSIX recommendation), as in:
my_prog -v -- -x hello world
It works well again.
However, a few questions remain:
Firstly, as far as I can tell the use of '--' is not documented in the
argparse documentation (but the code of argparse clearly shows that it
is a feature).
Secondly, when using '--', this string itself is inserted into the
list of the collected positional arguments (in the above example in
the cmd_args variable):
$ ./my_prog -v -- -x hello world
Namespace(cmd_args=['--', '-x', 'hello', 'world'], verbose=True)
It's quiet easy to check for it and remove it if it exists, but it
still seems like it shouldn't be there - it is not really an argument,
just a delimiter.
Am I doing anything wrong, or is this a bug? I hope someone here can
shed some light on this.
Thanks,
Amit
--
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
argparse: use of double dash to separate options and positional arguments Amit Ramon <amit.ramon@gmail.com> - 2015-11-06 12:07 +0200
csiph-web