I am writing a script that can do multiple things, depending on the arguments passed into the command line. For example:
#changed and simplified from actual code
parser.add_argument('-a','--apple')
parser.add_argument('-b','--boy')
parser.add_argument('-c', '--car')
parser.add_argument('-d', '--dog')
args = parser.parse_args()
I only want 1 argument in the commandline. So, only one of -a,-b,-c,-d would be accepted, and then the code will execute based on whichever argument was passed.
I can do this by a series of if statements, like
if args.a:
if args.b:
etc. Is there a better way to approach this?
For each command line option (-a,-b,-c,-d), I would like to have some arguments passed in. These arguments would be specific and would vary depending on the initial command line option. For example:
python test.py -a apple aardvark aquaman
I considered using nargs, but I was not sure how to make each argument was
- in the correct order and
- the argument that is needed. In the above example, assuming the number of extra optional arguments would be 3, I want to make sure that all the arguments start with the letter
a.
How could I approach this?