E.F
E.F

Reputation: 199

Combination of letters as parameters in python

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

Answers (1)

user2201041
user2201041

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

Related Questions