specifying the same argument multiple times with argparse
Gary Herron
gherron at digipen.edu
Mon Apr 16 18:19:26 EDT 2018
On 04/16/2018 02:31 PM, larry.martell at gmail.com wrote:
> Is there a way using argparse to be able to specify the same argument
> multiple times and have them all go into the same list?
>
> For example, I'd like to do this:
>
> script.py -foo bar -foo baz -foo blah
>
> and have the dest for foo have ['bar', 'baz', 'blah']
From the argparse web page
(https://docs.python.org/3/library/argparse.html):
'append' - This stores a list, and appends each argument value to the
list. This is useful to allow an option to be specified multiple times.
Example usage:
>>>
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='append')
>>> parser.parse_args('--foo 1 --foo 2'.split())
Namespace(foo=['1', '2'])
I hope that helps.
--
Dr. Gary Herron
Professor of Computer Science
DigiPen Institute of Technology
(425) 895-4418
More information about the Python-list
mailing list