- Python Home
- About
- News
- Documentation
- Downloads
- Community
- Foundation
- Developer's Guide
- Issue Tracker
- Issues
- Summaries
- User
- Administration
- Help
Issue26196
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2016-01-25 11:25 by Charles Daffern, last changed 2022-04-11 14:58 by admin. This issue is now closed.
| Messages (3) | |||
|---|---|---|---|
| msg258897 - (view) | Author: Charles Daffern (Charles Daffern) | Date: 2016-01-25 11:25 | |
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.
|
|||
| msg258898 - (view) | Author: SilentGhost (SilentGhost) * ![]() |
Date: 2016-01-25 11:48 | |
If you're to drop the space between the argument and its value, e.g. try_args('-a-a'), it seems to work as intended. The third example (
try_args("-a", "--")) still wouldn't work, but it looks like a reasonable workaround to me.
|
|||
| msg258910 - (view) | Author: Eric V. Smith (eric.smith) * ![]() |
Date: 2016-01-25 17:14 | |
I believe this is a duplicate of issue 9334. There's a lot of discussion there. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:58:26 | admin | set | github: 70384 |
| 2016-06-15 10:04:10 | spaceone | set | nosy:
+ spaceone |
| 2016-01-25 17:14:25 | eric.smith | set | status: open -> closed nosy: + eric.smith messages: + msg258910 superseder: argparse does not accept options taking arguments beginning with dash (regression from optparse) stage: resolved |
| 2016-01-25 11:48:59 | SilentGhost | set | nosy:
+ SilentGhost, bethard messages: + msg258898 |
| 2016-01-25 11:25:35 | Charles Daffern | create | |



