[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

specifying the same argument multiple times with argparse

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 

'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