Here is my argparse sample say sample.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-p", nargs="+", help="Stuff")
args = parser.parse_args()
print args
Python - 2.7.3
I expect that the user supplies a list of arguments separated by spaces after the -p option. For example, if you run
$ sample.py -p x y
Namespace(p=['x', 'y'])
But my problem is that when you run
$ sample.py -p x -p y
Namespace(p=['y'])
Which is neither here nor there. I would like one of the following
- Throw an exception to the user asking him to not use -p twice instead just supply them as one argument
- Just assume it is the same option and produce a list of ['x','y'].
I can see that python 2.7 is doing neither of them which confuses me. Can I get python to do one of the two behaviours documented above?
To produce a list of ['x','y'] use
action='append'
. Actually it givesFor each
-p
it gives a list['x']
as dictated bynargs='+'
, butappend
means, add that value to what the Namespace already has. The default action just sets the value, e.g.NS['p']=['x']
. I'd suggest reviewing theaction
paragraph in the docs.optionals
allow repeated use by design. It enables actions likeappend
andcount
. Usually users don't expect to use them repeatedly, or are happy with the last value.positionals
(without the-flag
) cannot be repeated (except as allowed bynargs
).How to add optional or once arguments? has some suggestions on how to create a 'no repeats' argument. One is to create a custom
action
class.I ran into the same issue. I decided to go with the custom action route as suggested by mgilson.
This results in
Still, it would have been much neater if there was an
action='extend'
option in the library by default.