I need some help regarding using argparse. What I want to achieve is that I need to pass in only one argument, it could be one of the followings: --k, --r, --b, --p,(ignore the rest). If the argument count is not 1, print “usage” information and quit. Also the program needs to know which flag is passed in in order to create corresponding object. I tried several times but I doesn’t work, can anyone give me a hint on this? Thanks.
I need some help regarding using argparse. What I want to achieve is that
Share
What you need to use to accomplish that is a mutually exclusive group:
As it can be seen in the example below, only one option in a mutually exclusive group is allowed at the same time:
To check which flag was passed, you just need to look at the
argparse.Namespaceobject returned byparse_argsmethod (the flag passed will be set toTrue).