Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98374 > unrolled thread
| Started by | Random832 <random832@fastmail.com> |
|---|---|
| First post | 2015-11-06 16:56 -0500 |
| Last post | 2015-11-06 16:56 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: argparse: use of double dash to separate options and positional arguments Random832 <random832@fastmail.com> - 2015-11-06 16:56 -0500
| From | Random832 <random832@fastmail.com> |
|---|---|
| Date | 2015-11-06 16:56 -0500 |
| Subject | Re: argparse: use of double dash to separate options and positional arguments |
| Message-ID | <mailman.100.1446847059.16136.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web