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.

Title: Argparse breaks when a switch is given an argument beginning with a dash
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
Status: closed Resolution:
Dependencies: Superseder: argparse does not accept options taking arguments beginning with dash (regression from optparse)
View: 9334
Assigned To: Nosy List: Charles Daffern, SilentGhost, bethard, eric.smith, spaceone
Priority: normal Keywords:

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()
    print("Trying:", args)
    except SystemExit:

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()
    print("Trying:", args)
    except SystemExit:

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) * (Python triager) 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) * (Python committer) Date: 2016-01-25 17:14
I believe this is a duplicate of issue 9334. There's a lot of discussion there.
Date User Action Args
2022-04-11 14:58:26adminsetgithub: 70384
2016-06-15 10:04:10spaceonesetnosy: + spaceone
2016-01-25 17:14:25eric.smithsetstatus: 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:59SilentGhostsetnosy: + SilentGhost, bethard
messages: + msg258898
2016-01-25 11:25:35Charles Dafferncreate