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

Random832 random832 at fastmail.com
Fri Nov 6 16:56:50 EST 2015


Peter Otten <__peter__ at 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"





More information about the Python-list mailing list