This looks like a bug. You should report it.
This code in argparse.py
is the start of _get_values
, one of the primary helper functions for parsing values:
if action.nargs not in [PARSER, REMAINDER]:
try:
arg_strings.remove('--')
except ValueError:
pass
The code receives the --
argument as the single element of a list ['--']
. It tries to remove '--'
from the list, because when using --
as an end-of-options marker, the '--'
string will end up in arg_strings
for one of the _get_values
calls. However, when '--'
is the actual argument value, the code still removes it anyway, so arg_strings
ends up being an empty list instead of a single-element list.
The code then goes through an else-if chain for handling different kinds of argument (branch bodies omitted to save space here):
# optional argument produces a default when not present
if not arg_strings and action.nargs == OPTIONAL:
...
# when nargs='*' on a positional, if there were no command-line
# args, use the default if it is anything other than None
elif (not arg_strings and action.nargs == ZERO_OR_MORE and
not action.option_strings):
...
# single argument or optional argument produces a single value
elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
...
# REMAINDER arguments convert all values, checking none
elif action.nargs == REMAINDER:
...
# PARSER arguments convert all values, but check only the first
elif action.nargs == PARSER:
...
# SUPPRESS argument does not put anything in the namespace
elif action.nargs == SUPPRESS:
...
# all other types of nargs produce a list
else:
...
This code should go through the 3rd branch,
# single argument or optional argument produces a single value
elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
but because the argument is missing from arg_strings
, len(arg_strings)
is 0. It instead hits the final case, which is supposed to handle a completely different kind of argument. That branch ends up returning an empty list instead of the '--'
string that should have been returned, which is why args.delimiter
ends up being an empty list instead of a '--'
string.
This bug manifests with positional arguments too. For example,
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('a')
parser.add_argument('b')
args = parser.parse_args(["--", "--", "--"])
print(args)
prints
Namespace(a='--', b=[])
because when _get_values
handles the b
argument, it receives ['--']
as arg_strings
and removes the '--'
. When handling the a
argument, it receives ['--', '--']
, representing one end-of-options marker and one actual --
argument value, and it successfully removes the end-of-options marker, but when handling b
, it removes the actual argument value.
--option=--
and--option='--'
both become the exact same argv elements while the shell is parsing them (before Python is even started).--
should be special only completely on its own, as an exact match;--delimiter=--
is not.print(args)
instead ofprint(args.delimiter)
.args.delimiter
becomes a list instead of a string. It really shouldn't ever be anything but a string or None (with None being the value if you don't provide the option at all).