Have you tried using --
to separate the --foo
argument(s) from the bar
?
With this basic setup:
p=argparse.ArgumentParser()
p.add_argument('--foo',nargs='?',const='abc', default='other')
p.add_argument('bar')
In [633]: p.parse_args(['bar'])
Out[633]: Namespace(bar='bar', foo='other')
In [634]: p.parse_args(['bar','--foo'])
Out[634]: Namespace(bar='bar', foo='abc')
In [635]: p.parse_args(['bar','--foo','test'])
Out[635]: Namespace(bar='bar', foo='test')
In [636]: p.parse_args(['--foo','bar'])
usage: ipython3 [-h] [--foo [FOO]] bar
'error message'
The plain --foo
can occur AFTER bar
. 'other' ('default) is the value it gets if absent, 'abc' (the
const`) the value it gets if present but 'empty'.
But as you found out, when --foo
is first it consumes the following string, leaving nothing for the positional
argument. In other words, when handling --foo
, it does not take into account the future needs of bar
.
If I add another argument
p.add_argument('--baz',action='store_true')
p.parse_args(['--foo','--baz','bar'])
# Namespace(bar='bar', baz=True, foo='abc')
This works because --baz
marks the end of --foo
arguments.
You can also use --
to mark the end of optionals
and the start of postionals
:
p.parse_args(['--foo','--','bar'])
There is a bug issue
that tries to rectify this by reserving strings for trailing positionals
http://bugs.python.org/issue9338 argparse optionals with nargs='?', '*' or '+' can't be followed by positionals
But the patch is not trivial. --
is your best tool at this time.
I don't follow your comment to Martijn about mutually exclusive group(s).