Re: specifying the same argument multiple times with argparse

2018-04-16 Thread Larry Martell
On Mon, Apr 16, 2018 at 6:19 PM, Gary Herron  wrote:
> On 04/16/2018 02:31 PM, larry.mart...@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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: specifying the same argument multiple times with argparse

2018-04-16 Thread Gary Herron

On 04/16/2018 02:31 PM, larry.mart...@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

--
https://mail.python.org/mailman/listinfo/python-list


Re: specifying the same argument multiple times with argparse

2018-04-16 Thread Peter Otten
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']


-- 
https://mail.python.org/mailman/listinfo/python-list