2011-10-23 14 views
47

Python2.7 argparse sólo acepta argumentos opcionales (con el prefijo) en grupos mutuamente excluyentes:¿Cómo hacer que python argparse argumentos de grupo mutuamente excluyentes sin prefijo?

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 

¿Hay una manera de hacer argumentos argparse comporta como control tradicional daemon UNIX :

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

Respuesta

52

Para todas las capacidades y opciones en argparse no creo que jamás obtener una cadena de uso que "enlatados" miradas como lo que quieres

Dicho esto, ¿ha mirado sub-analizadores desde su publicación original?

Aquí hay una aplicación de barebones:

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

La ejecución de este con los rendimientos -h opción:

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

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

Uno de los beneficios de este enfoque es ser capaz de utilizar set_defaults para cada sub-analizador para conectar una función directamente al argumento. También he añadido una opción de "gracia" para stop y 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 el mensaje de "ayuda" para 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 

Parar "con gracia":

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

Pero no muestra cómo determinar qué opciones de inicio de parada o reinicio se seleccionaron. Cuando trato de ver la representación de los argumentos, no se muestran ninguno de los argumentos del sub-analizador. –

33

Parece que desea un argumento posicional en lugar de opciones mutuamente excluyentes. Puede usar 'elecciones' para restringir las posibles opciones aceptables.

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

Esto produce una línea de uso que tiene este aspecto:

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

Sí, lo vi, pero las opciones limitan la expresividad de uso. Solo estoy buscando una forma de deshacerme de los prefijos. –

+0

¿Qué quiere decir "límites de expresión de uso"? ¿Puede el usuario ejecutar el script sin proporcionar uno de estos? –

+0

Cuando el usuario emite "mydaemon -h", la ayuda (uso) no es tan clara como utilizar una cadena de ayuda para cada argumento. –

11

Basándose en la respuesta de Adam ... si quisiera especificar un valor por defecto, siempre podría hacer lo siguiente para dejarlo en blanco.

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

que imprimirá:

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

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

salida:

$ 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 

versión2

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 
Cuestiones relacionadas