Author eric.smith
Recipients andersk, bethard, drm, eric.araujo, eric.smith, gdb, nelhage, r.david.murray
Date 2011-02-06.15:56:11
SpamBayes Score 3.27524e-09
Marked as misclassified No
Message-id <1297007771.99.0.959456880057.issue9334@psf.upfronthosting.co.za>
In-reply-to
Content
"--" 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.
History
Date User Action Args
2011-02-06 15:56:12eric.smithsetrecipients: + eric.smith, bethard, eric.araujo, r.david.murray, andersk, gdb, nelhage, drm
2011-02-06 15:56:11eric.smithsetmessageid: <1297007771.99.0.959456880057.issue9334@psf.upfronthosting.co.za>
2011-02-06 15:56:11eric.smithlinkissue9334 messages
2011-02-06 15:56:11eric.smithcreate