2011-10-23 14 views
47

Python2.7 argparse accetta solo argomenti facoltativi (prefisso) in gruppi mutuamente esclusivi:Come rendere Python argparse argomenti di gruppo che si escludono a vicenda senza prefisso?

parser = argparse.ArgumentParser(prog='mydaemon') 
action = parser.add_mutually_exclusive_group(required=True) 
action.add_argument('--start', action='store_true', help='Starts %(prog)s daemon') 
action.add_argument('--stop', action='store_true', help='Stops %(prog)s daemon') 
action.add_argument('--restart', action='store_true', help='Restarts %(prog)s daemon') 

$ mydaemon -h

usage: mydaemon [-h] (--start | --stop | --restart) 

optional arguments: 
    -h, --help show this help message and exit 
    --start  Starts mydaemon daemon 
    --stop  Stops mydaemon daemon 
    --restart Restarts mydaemon daemon 

C'è un modo per rendere gli argomenti argparse si comporta come il controllo tradizionale daemon UNIX :

(start | stop | restart) and not (--start | --stop | --restart) ? 

risposta

52

Per tutte le capacità e le opzioni in argparse non credo che tu abbia mai ottenere una stringa di utilizzo che "in scatola" sguardi come quello che vuoi

Detto questo, hai guardato sotto-parser dal tuo post originale?

Ecco un'implementazione barebone:

import argparse 

parser = argparse.ArgumentParser(prog='mydaemon') 
sp = parser.add_subparsers() 
sp_start = sp.add_parser('start', help='Starts %(prog)s daemon') 
sp_stop = sp.add_parser('stop', help='Stops %(prog)s daemon') 
sp_restart = sp.add_parser('restart', help='Restarts %(prog)s daemon') 

parser.parse_args() 

L'esecuzione di questo con i rendimenti -h opzione:

usage: mydaemon [-h] {start,stop,restart} ... 

positional arguments: 
    {start,stop,restart} 
    start    Starts mydaemon daemon 
    stop    Stops mydaemon daemon 
    restart    Restarts mydaemon daemon 

Uno dei vantaggi di questo approccio è in grado di utilizzare set_defaults per ogni sotto-parser per agganciare una funzione direttamente all'argomento. Ho anche aggiunto un'opzione "graziosa" per stop e restart:

import argparse 

def my_stop(args): 
    if args.gracefully: 
     print "Let's try to stop..." 
    else: 
     print 'Stop, now!' 

parser = argparse.ArgumentParser(prog='mydaemon') 

graceful = argparse.ArgumentParser(add_help=False) 
graceful.add_argument('-g', '--gracefully', action='store_true', help='tries to terminate the process gracefully') 
sp = parser.add_subparsers() 
sp_start = sp.add_parser('start', help='Starts %(prog)s daemon') 
sp_stop = sp.add_parser('stop', parents=[graceful], 
        description='Stops the daemon if it is currently running.', 
        help='Stops %(prog)s daemon') 
sp_restart = sp.add_parser('restart', parents=[graceful], help='Restarts %(prog)s daemon') 

sp_stop.set_defaults(func=my_stop) 

args = parser.parse_args() 
args.func(args) 

Mostrando il messaggio "help" per stop:

$ python mydaemon.py stop -h 
usage: mydaemon stop [-h] [-g] 

Stops the daemon if it is currently running. 

optional arguments: 
    -h, --help  show this help message and exit 
    -g, --gracefully tries to terminate the process gracefully 

Arresto "grazia":

$ python mydaemon.py stop -g 
Let's try to stop... 
+0

Ma non viene mostrato come determinare quale start stop o riavvio sono state selezionate. Quando provo a visualizzare il repr degli argomenti, non viene mostrato nessuno degli argomenti relativi ai parser secondari. –

33

Sembra che si desideri un argomento posizionale anziché opzioni mutuamente esclusive. Puoi usare 'scelte' per limitare le possibili opzioni accettabili.

parser = ArgumentParser() 
parser.add_argument('action', choices=('start', 'stop', 'restart')) 

Questo produce una linea di utilizzo simile a questa:

usage: foo.py [-h] {start,stop,restart} 
+0

Sì, ho visto che, ma le scelte limiti expresivity di utilizzo. Sto solo cercando un modo per sbarazzarmi dei prefissi. –

+0

Cosa intendi con "limiti espressività di utilizzo"? L'utente può eseguire lo script senza fornire uno di questi? –

+0

Quando l'utente emette "mydaemon -h" l'aiuto (utilizzo) non è così chiaro come usare una stringa di aiuto per ogni argomento. –

11

Basandosi sulla risposta di Adam ... se si volesse specificare un valore predefinito si potrebbe sempre fare quanto segue in modo che possano effettivamente lasciare vuoto.

import argparse 

ActionHelp = """ 
    Start = Starts the daemon (default) 
    Stop = Stops the daemon 
    Restart = Restarts the daemon 
    """ 
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) 

parser.add_argument('action', nargs = '?', choices=('start', 'stop', 'restart'), 
    default = 'start', help = ActionHelp) 

print parser.parse_args(''.split()) 
print 
print parser.parse_args('-h'.split()) 

che stamperà:

Namespace(action='start') 

usage: program.py [-h] [{start,stop,restart}] 

postional arguments: 
    {start,stop,restart} 
         Start = Starts the daemon (default) 
         Stop = Stops the daemon 
         Restart = Restarts the daemon 

optional arguments: 
    -h, --help  show this help message and exit 
27

da pymotw

import argparse 

parser = argparse.ArgumentParser() 

group = parser.add_mutually_exclusive_group() 
group.add_argument('-a', action='store_true') 
group.add_argument('-b', action='store_true') 

print parser.parse_args() 

uscita:

$ python argparse_mutually_exclusive.py -h 
usage: argparse_mutually_exclusive.py [-h] [-a | -b] 

optional arguments: 
    -h, --help show this help message and exit 
    -a 
    -b 

$ python argparse_mutually_exclusive.py -a 
Namespace(a=True, b=False) 

$ python argparse_mutually_exclusive.py -b 
Namespace(a=False, b=True) 

$ python argparse_mutually_exclusive.py -a -b 
usage: argparse_mutually_exclusive.py [-h] [-a | -b] 
argparse_mutually_exclusive.py: error: argument -b: not allowed with argument -a 

version2

import argparse 

parser = argparse.ArgumentParser() 

subparsers = parser.add_subparsers(help='commands') 

# A list command 
list_parser = subparsers.add_parser('list', help='List contents') 
list_parser.add_argument('dirname', action='store', help='Directory to list') 

# A create command 
create_parser = subparsers.add_parser('create', help='Create a directory') 
create_parser.add_argument('dirname', action='store', help='New directory to create') 
create_parser.add_argument('--read-only', default=False, action='store_true', 
         help='Set permissions to prevent writing to the directory', 
         ) 

# A delete command 
delete_parser = subparsers.add_parser('delete', help='Remove a directory') 
delete_parser.add_argument('dirname', action='store', help='The directory to remove') 
delete_parser.add_argument('--recursive', '-r', default=False, action='store_true', 
         help='Remove the contents of the directory, too', 
         ) 

print parser.parse_args(['list', 'a s d', ]) 
>>> Namespace(dirname='a s d') 
print parser.parse_args(['list', 'a s d', 'create' ]) 
>>> error: unrecognized arguments: create 
Problemi correlati