Message128062
"--" 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. |
|
Date |
User |
Action |
Args |
2011-02-06 15:56:12 | eric.smith | set | recipients:
+ eric.smith, bethard, eric.araujo, r.david.murray, andersk, gdb, nelhage, drm |
2011-02-06 15:56:11 | eric.smith | set | messageid: <1297007771.99.0.959456880057.issue9334@psf.upfronthosting.co.za> |
2011-02-06 15:56:11 | eric.smith | link | issue9334 messages |
2011-02-06 15:56:11 | eric.smith | create | |
|