I have a program that accepts a file as input, does some work with the contents of the file and the pushes it out to a server. I want to add an optional command line switch to specify a “dry run” of the program whereby it does all the file crunching, but skips doing the writes out to the server. I am using argparse to bring in the command line arguments, but I don’t see a way to do an “OR” function between the arguments. Here is what I’m more or less looking for…
Allowable options:
1) prog.py inputfile servername
2) prog.py inputfile -d
3) prog.py inputfile -d servername
Dissallowed:
1) prog.py inputfile
I want to ensure that either the server name “OR” the dry run flag are on the command line. And, if both are there… that’s OK too. (hence being an OR and not an XOR). If I use mutually exclusive with required=true, I can get the XOR; but, I can’t seem to figure out how to do this as an “OR” where both can be present. To complicate matters, the server name is a positional argument and the dry run flag is an optional argument that could be anywhere on the command line. Does anyone have an idea on how to pull this off?
Here’s what I would do:
Examples:
You could also do this using a custom action and it has the same effect: