Domanda

Sto scrivendo una sceneggiatura che ha 2 argomenti che si escludono a vicenda, e un'opzione che ha senso solo con uno di questi argomenti. Sto cercando di impostare argparse a fallire se lo si chiama con l'argomento che non ha senso.

Per essere chiari:

-m -f senso

-s senso

-s -f dovrebbe gettare errori

senza argomenti vanno bene.

Il codice che ho è:

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')

Il che non funziona, in quanto sputa fuori

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

, piuttosto che quello che mi aspetto:

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

o somesuch.

 whichboom -s -f -m 116

anche non gettare eventuali errori.

È stato utile?

Soluzione

Devi solo i gruppi di argomenti mescolati. Nel codice, si assegna una sola opzione al gruppo escludono a vicenda. Penso che quello che vuoi è:

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')

Si potrebbe semplicemente ignorare il tutto mutuamente esclusive gruppo e aggiungere qualcosa di simile:

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()

Altri suggerimenti

Aggiungere l'argomento usage durante la creazione del 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)

Fonte: http://docs.python.org/dev/library/ argparse.html # utilizzo

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top