Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98346
| Path | csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Amit Ramon <amit.ramon@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: argparse: use of double dash to separate options and positional arguments |
| Date | Fri, 6 Nov 2015 13:44:59 +0200 |
| Lines | 75 |
| Message-ID | <mailman.82.1446810309.16136.python-list@python.org> (permalink) |
| References | <20151106100743.GN3685@isis.luna> <n1i126$o2f$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii; format=flowed |
| X-Trace | news.uni-berlin.de r4aEcM6uZPzPHuRBC7fPTwQFFs79RaRSbu9D0u5a0/Rg== |
| 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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'receives': 0.03; '(so': 0.07; 'convention.': 0.07; 'option,': 0.07; 'unrecognized': 0.07; "'-'": 0.09; 'argument,': 0.09; 'delimiter': 0.09; 'inserted': 0.09; 'okay': 0.09; 'positional': 0.09; 'bug': 0.10; '(but': 0.15; 'argument': 0.15; 'subject: \n ': 0.15; 'argparse': 0.16; 'arguments:': 0.16; 'complains': 0.16; 'expects': 0.16; 'posix': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:> 20': 0.16; 'subject:argparse': 0.16; 'subject:options': 0.16; 'string': 0.17; 'documented': 0.18; 'example.': 0.18; "shouldn't": 0.18; '(in': 0.18; 'arguments': 0.22; 'bug?': 0.22; 'cheers,': 0.22; 'pass': 0.22; 'decide': 0.23; 'seems': 0.23; 'passing': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'error': 0.27; 'looks': 0.29; 'exists,': 0.29; 'invoke': 0.29; 'quiet': 0.29; 'starts': 0.29; 'program,': 0.29; 'received:209.85.212': 0.29; "i'm": 0.30; 'code': 0.30; 'another': 0.32; 'options': 0.33; 'common': 0.33; 'foo': 0.33; 'handle': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'subject:use': 0.35; 'but': 0.36; 'should': 0.36; 'needed': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'expect': 0.37; 'say': 0.37; 'charset:us-ascii': 0.37; 'itself': 0.38; 'received:209': 0.38; 'delete': 0.38; 'files': 0.38; 'end': 0.39; 'why': 0.39; 'sure': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'still': 0.40; 'questions': 0.40; 'easy': 0.60; 'your': 0.60; 'world': 0.64; 'special': 0.73; 'one;': 0.84; 'otten': 0.84; 'unclear': 0.84; 'received:185': 0.91; 'safer': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=uhE4OjNpJWtB9rf1WcWnxyPFbPkq7gUyKMCN3LPpxc4=; b=HHdjzgOj4IfkuTRgoGTB++96+/M20olsHbFhpNyYICi/2n+vRcM4qmFdrO4AUahefd haYquiprBRyqQCH3dc/uYcHsjBAIp4wl0wz3yS/sEWjCKTz3ysj7zsE9+yW5zmk3VWZH Vow+aLFmMeAxH2er6Fr+u3owiiwhnHTRqstDqVlQIMI9k+IQCup3oFLo0x1P5+CJt5zo AF4GwHsJ8AIVwHaAZgEbkghPO766K0BRM6+cqM4kgqVPRLXWb8L4g4FS786taBuNM257 5YqYLE20tY3AWXgfPGFM+Vt7IWXceUaLLuvLrMtLWIXHTZbV5awMwmtzsC2ZqBnpU99J yiMw== |
| X-Received | by 10.194.134.199 with SMTP id pm7mr13951929wjb.46.1446810302204; Fri, 06 Nov 2015 03:45:02 -0800 (PST) |
| Content-Disposition | inline |
| In-Reply-To | <n1i126$o2f$1@ger.gmane.org> |
| User-Agent | Mutt/1.5.24 (2015-08-30) |
| 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:98346 |
Show key headers only | View raw
Peter Otten <__peter__@web.de> [2015-11-06 11:57 +0100]:
>>
>> 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'.
>
>This looks like a bug to me. Please report it on bug.python.org.
Why does it looks like a bug? To me it seems okay - argparse expects
that an argument that starts with '-' is an option, and it also expects
to be told about all possible options, so it complains on
encountering an unrecognized 'option'. This is why a special delimiter
is needed to mark the 'end of options'.
>
>> 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.
>
>I'm not sure about this one; one purpose of REMAINDER is to pass on the
>unprocessed arguments to another program/script, and this might follow the
>same convention. Should
>
>parser.add_argument('-v', '--verbose', action='store_true')
>parser.add_argument('cmd_args', nargs=argparse.REMAINDER)
>args = parser.parse_args()
>subprocess.call(["rm"] + args.cmd_args)
>
>$ my_prog -v -- -r foo
>
>attempt to delete two files "-r" and "foo" or remove the "foo" directory?
>The first is the safer alternative, and as you say stripping the "--" is
>easy.
I'm using the REMAINDER exactly for passing on arguments to another
program, as in your example. I would expect that everything after
the '--' should be passed on as is - it should be up to the other
program to decide what to do with the arguments it receives (So in
your examples, rm would actually try to remove the "foo" directory).
And although stripping the '--' is easy, why should the user of
argparse need to handle it? Still unclear to me.
Cheers,
Amit
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: argparse: use of double dash to separate options and positional arguments Amit Ramon <amit.ramon@gmail.com> - 2015-11-06 13:44 +0200
csiph-web