Eric Smith <[email protected]> added the comment:
"--" won't work. Traditionally, this has been used to separate optional
arguments from positional arguments. Continuing the "cd" example, that's what
would let you cd into a directory whose name starts with a hyphen:
$ cd -links-/
-bash: cd: -l: invalid option
cd: usage: cd [-L|-P] [dir]
$ cd -- -links-
$
This would also work with argparse:
import argparse
parser = argparse.ArgumentParser(prog='cd')
parser.add_argument('-L', help='follow symbolic links')
parser.add_argument('-P', help='do not follow symbolic links')
parser.add_argument('dir', help='directory name')
print(parser.parse_args(['--', '-Links-']))
prints:
Namespace(L=None, P=None, dir='-Links-')
Continuing the example from my earlier post shows it won't work for values for
optional arguments:
>>> parser.parse_args(['--asciidoc-opts -- -one'])
usage: a2x [-h] [--asciidoc-opts ASCIIDOC_OPTS]
a2x: error: unrecognized arguments: --asciidoc-opts -- -one
I believe it's only the '=' that will solve this problem. In fact, because of
this issue, I suggest we document '=' as the preferred way to call argparse
when optional arguments have values, and change all of the examples to use it.
I also think it would cause less confusion (because of this issue) if the help
output showed the equal sign. But I realize that's probably more controversial.
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue9334>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com