Argparse: add_argument with action=append appends to default

John O'Hagan research at
Thu Sep 16 05:18:56 CEST 2010

I find this surprising:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--test', action='append', default=['default'])
>>> parser.parse_args(['--test', 'arg'])
Namespace(test=['default', 'arg'])

As an argument is provided, I didn't expect to see the default in there as 
well. From the argparse docs: "the default value is used when the option 
string was not present at the command line". While it doesn't say "_only_ 
when...", that's what I would have expected. 

Is there a reason for this behaviour? Is there a way to do what I want, to get 
a list of appended options _or_ a default list (apart from setting defaults 
outside the parser, a good way to lose track of things)?



More information about the Python-list mailing list