Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python argparse with dependencies

I'm writing a script which has 2 arguments which are mutually exclusive, and an option that only makes sense with one of those arguments. I'm trying to set up argparse to fail if you call it with the argument that makes no sense.

To be clear:

-m -f makes sense

-s makes sense

-s -f should throw errors

no arguments are fine.

The code I have is:

parser = argparse.ArgumentParser(description='Lookup servers by ip address from host file')
parser.add_argument('host', nargs=1,
            help="ip address to lookup")
main_group = parser.add_mutually_exclusive_group()
mysql_group = main_group.add_argument_group()
main_group.add_argument("-s", "--ssh", dest='ssh', action='store_true',
            default=False,
            help='Connect to this machine via ssh, instead of printing hostname')
mysql_group.add_argument("-m", "--mysql", dest='mysql', action='store_true',
            default=False,
            help='Start a mysql tunnel to the host, instead of printing hostname')
mysql_group.add_argument("-f", "--firefox", dest='firefox', action='store_true',
            default=False,
            help='Start a firefox session to the remotemyadmin instance')

Which doesn't work, as it spits out

 usage: whichboom [-h] [-s] [-m] [-f] host

rather than what I'd expect:

 usage: whichboom [-h] [-s | [-h] [-s]] host

or somesuch.

 whichboom -s -f -m 116

also doesn't throw any errors.

like image 285
richo Avatar asked Dec 16 '10 22:12

richo


2 Answers

You just have the argument groups mixed up. In your code, you only assign one option to the mutually exclusive group. I think what you want is:

parser = argparse.ArgumentParser(description='Lookup servers by ip address from host file')
parser.add_argument('host', nargs=1,
            help="ip address to lookup")
main_group = parser.add_mutually_exclusive_group()
mysql_group = main_group.add_argument_group()
main_group.add_argument("-s", "--ssh", dest='ssh', action='store_true',
            default=False,
            help='Connect to this machine via ssh, instead of printing hostname')
mysql_group.add_argument("-m", "--mysql", dest='mysql', action='store_true',
            default=False,
            help='Start a mysql tunnel to the host, instead of printing hostname')
main_group.add_argument("-f", "--firefox", dest='firefox', action='store_true',
            default=False,
            help='Start a firefox session to the remotemyadmin instance')

You could just skip the whole mutually exclusive group thing and add something like this:

usage = 'whichboom [-h] [-s | [-h] [-s]] host'
parser = argparse.ArgumentParser(description, usage)
options, args = parser.parse_args()
if options.ssh and options.firefox:
    parser.print_help()
    sys.exit()
like image 141
user545424 Avatar answered Oct 24 '22 10:10

user545424


Add the usage argument when creating the parser:

usage = "usage: whichboom [-h] [-s | [-h] [-s]] host"
description = "Lookup servers by ip address from host file"
parser = argparse.ArgumentParser(description=description, usage=usage)

Source: http://docs.python.org/dev/library/argparse.html#usage

like image 26
Andrew Clark Avatar answered Oct 24 '22 09:10

Andrew Clark