specifying the same argument multiple times with argparse

Larry Martell larry.martell at gmail.com
Mon Apr 16 19:33:29 EDT 2018


On Mon, Apr 16, 2018 at 6:19 PM, Gary Herron <gherron at digipen.edu> wrote:
> 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'])

Thanks! I did not see that in the docs.



More information about the Python-list mailing list