Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR function with argparse with two variables on the command line in Python

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?

like image 747
Craig Avatar asked Sep 21 '12 18:09

Craig


2 Answers

Here's what I would do:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--dry-run', action='store_true')
parser.add_argument('input_file', type=argparse.FileType('r'))
parser.add_argument('servername', nargs='?')

args = parser.parse_args()
if args.servername is None and not args.dry_run:
    parser.error("Option 'servername' is required when not in dry-run mode.")

print args

Examples:

$ ./prog.py inputfile servername
Namespace(dry_run=False, input_file=<open file 'inputfile', mode 'r' at 0x283440>, servername='servername')
$ ./prog.py inputfile -d
Namespace(dry_run=True, input_file=<open file 'inputfile', mode 'r' at 0x2cf440>, servername=None)
$ ./prog.py -d inputfile servername
Namespace(dry_run=True, input_file=<open file 'inputfile', mode 'r' at 0x1f4440>, servername='servername')
$ ./prog.py inputfile
usage: prog.py [-h] [-d] input_file [servername]
prog.py: error: Option 'servername' is required when not in dry-run mode.

You could also do this using a custom action and it has the same effect:

class ServernameAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        if values is None and not namespace.dry_run:
            parser.error("Option 'servername' is required when not in dry-run mode.")
        setattr(namespace, self.dest, values)

...
parser.add_argument('servername', nargs='?', action=ServernameAction)
...
like image 59
jterrace Avatar answered Oct 15 '22 10:10

jterrace


argparse can't do every possible combination of validation, so it's worth learning how to add your own validation after the fact. For example:

parser = argparse.ArgumentParser(epilog='Either foo or bar (or both) must be specified.')
parser.add_argument('--foo', help='do something.')
parser.add_argument('--bar', help='do something else.')
args = vars(parser.parse_args())
if not args['foo'] and not args['bar']:
  parser.error('Either foo or bar (or both) must be specified.')
like image 25
abarnert Avatar answered Oct 15 '22 12:10

abarnert