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


Groups > comp.lang.python > #98374

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

From Random832 <random832@fastmail.com>
Newsgroups comp.lang.python
Subject Re: argparse: use of double dash to separate options and positional arguments
Date 2015-11-06 16:56 -0500
Message-ID <mailman.100.1446847059.16136.python-list@python.org> (permalink)
References <20151106100743.GN3685@isis.luna> <n1i126$o2f$1@ger.gmane.org>

Show all headers | View raw


Peter Otten <__peter__@web.de> writes:

> 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 think it should be the second. If the caller wants it to be treated as
a list of files rather than a list of arguments, it should be using
subprocess.call(["rm", "--"] + args.cmd_args).

The purpose is, as you said, to pass the *unprocessed* argument. -- has
been processed by being interpreted as a terminator for the argument
list.

I have a script (it's a shell script, not a python script) where I
actually do something similar... it does some setup, then calls ssh. If
I want to pass some options directly to ssh, I call it as:
"scriptname -script-option -- -ssh-option remote-command"

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


Thread

Re: argparse: use of double dash to separate options and positional arguments Random832 <random832@fastmail.com> - 2015-11-06 16:56 -0500

csiph-web