Example code demonstrating the problem:
# {{{
import argparse
def try_args(*args):
parser = argparse.ArgumentParser()
parser.add_argument("-a")
print("Trying:", args)
try:
print(parser.parse_args(args))
except SystemExit:
print("FAILED!")
try_args("-a", "-") # Works fine
try_args("-a", "-a") # Breaks
try_args("-a", "--") # Breaks
try_args("-a", "--things--") # Breaks
# }}}
This behaviour is contrary to optparse:
# {{{
import optparse
def try_args(*args):
parser = optparse.OptionParser()
parser.add_option("-a")
print("Trying:", args)
try:
print(parser.parse_args(list(args)))
except SystemExit:
print("FAILED!")
try_args("-a", "-") # Works
try_args("-a", "-a") # Works
try_args("-a", "--") # Works
try_args("-a", "--things--") # Works
# }}}
It is also contrary to many other utilities, including python itself:
# {{{
$ python -c -c
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'c' is not defined
$ printf 'hello\nworld\n-- pick me\netc\n' | grep -e --
-- pick me
$ gawk -f --
gawk: fatal: can't open source file `--' for reading (No such file or directory)
$ vim -u --
E282: Cannot read from "--"
Press ENTER or type command to continue
$ man -M --asdf man
No manual entry for man
$ less -b --sdfkds
Number is required after -b (--buffers)
Missing filename ("less --help" for help)
$ perl -e --fasd
Can't modify constant item in predecrement (--) at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
# }}}
I first encountered this problem when using a text scrolling utility someone had written in python, and tried to pass "--" as the text separator. The program just bailed out, and it turned out that it wasn't the author's fault but a problem in argparse itself.
|