Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argparse - Optional argument with list/tuple

I have several optional arguments and one of them needs to store multiple values - i.e. its type is neither string nor integer. Does argparse support optional arguments of other types?

For example, I would like this command:

>>> python example1 test_project --name try1 3

to be the equivalent of code like this:

args.name = {'try1', '3'}

Is this possible? Or will I be forced to use action='store_true' and then prompt the user for more information if he choose this option?

like image 292
Gil.I Avatar asked Sep 01 '25 01:09

Gil.I


1 Answers

I'm really having a hard time following your question. Is this what you want?

import argparse

parser=argparse.ArgumentParser()
parser.add_argument('--name',nargs='*',action='store')

print(parser.parse_args('--name try1 3'.split())) #Namespace(name=['try1', '3'])

If it isn't, perhaps you could try to be a little more clear with what you want to make argparse do, and what you've been able to do with it.

like image 134
mgilson Avatar answered Sep 02 '25 14:09

mgilson