Reputation: 199
I wondered if there is a way - didn't find in on the internet but I heard it was possible- to get many parameters in a python script as a combination of letters ? I explain myself : if the user wants to send many optional parameters Example : If the options are tiger, frog, cat and dog: instead of writing
python ./myScript.py --tiger --frog --cat --dog
To write :
python ./myScript.py --tfcd
Is there a way to do that ?
Thanks
Upvotes: 6
Views: 403
Reputation:
argparse
can take both short and long arguments. Short arguments can be combined "as long as only the last argument (or none of them) requires a value."
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--tiger', action='store_true')
parser.add_argument('-c', '--cat')
print(parser.parse_args())
Usage:
>> python test.py -tcToonses
Namespace(cat='Toonses', tiger=True)
Upvotes: 8