Why won't argparse parse these arguments?
--foo 1 2 3 bar
Using
parser = argparse.ArgumentParser()
parser.add_argument('--foo', nargs='+')
parser.add_argument('bar')
which gives the following error:
error: too few arguments
If I pass the bar argument first though, it works:
bar --foo 1 2 3
Now, this in itself is not too bad. I can live with having the positional arguments first it's just that this behaviour is inconsistent with the help argparse creates for us, which states that bar should be last:
usage: argparsetest.py [-h] [--foo FOO [FOO ...]] bar
So how do you make this work with consistent help text?
Here's a complete test program.
--to endnargsglobbing, so--foo 1 2 3 -- barshould work in your example above. It really should be solved automatically, reserving the number of arguments needed for positional arguments, in my opinion. There are discussions on this open issue at bugs.python.org/issue9338 and bugs.python.org/issue9182 (at the very least it should be clearly documented).--to stop the list is so cool. this is my favourite answer.