Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98345

Re: argparse: use of double dash to separate options and positional arguments

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: argparse: use of double dash to separate options and positional arguments
Date Fri, 06 Nov 2015 11:57:06 +0100
Organization None
Lines 73
Message-ID <mailman.81.1446807457.16136.python-list@python.org> (permalink)
References <20151106100743.GN3685@isis.luna>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de 1d1o0LRP/IDmcMxGMqzNlQVPbYjZ1JGYiccOACuDgV7w==
Return-Path <python-python-list@m.gmane.org>
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; 'args': 0.04; 'convention.': 0.07; 'unrecognized': 0.07; 'argument,': 0.09; 'inserted': 0.09; 'positional': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'wrong,': 0.09; 'bug': 0.10; '(but': 0.15; 'argument': 0.15; 'argparse': 0.16; 'arguments:': 0.16; 'posix': 0.16; 'ramon': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:argparse': 0.16; 'subject:options': 0.16; 'wrote:': 0.16; 'string': 0.17; 'documented': 0.18; "shouldn't": 0.18; '(in': 0.18; 'arguments': 0.22; 'bug?': 0.22; 'parser': 0.22; 'pass': 0.22; 'trying': 0.22; 'seems': 0.23; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'this.': 0.28; 'looks': 0.29; '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; 'another': 0.32; 'options': 0.33; 'common': 0.33; 'foo': 0.33; 'list': 0.34; 'gets': 0.35; 'attempt': 0.35; 'subject:use': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'say': 0.37; 'received:org': 0.37; 'doing': 0.38; 'itself': 0.38; 'delete': 0.38; 'anything': 0.38; 'skip:p 20': 0.38; 'someone': 0.38; 'files': 0.38; 'end': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:de': 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; 'one;': 0.84; 'safer': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd9c26.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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:98345

Show key headers only | View raw


Amit Ramon wrote:

> 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'.

This looks like a bug to me. Please report it on bug.python.org.
 
> 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.


> Am I doing anything wrong, or is this a bug? I hope someone here can
> shed some light on this.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: argparse: use of double dash to separate options and positional arguments Peter Otten <__peter__@web.de> - 2015-11-06 11:57 +0100

csiph-web