specifying the same argument multiple times with argparse

Peter Otten __peter__ at web.de
Mon Apr 16 18:01:25 EDT 2018


Larry Martell 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?

action="append"

 
> 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']

$ cat script.py
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--foo", action="append", default=[])
print(parser.parse_args().foo)
$ ./script.py --foo one --foo two --foo three
['one', 'two', 'three']




More information about the Python-list mailing list