简体   繁体   English

可选的python参数没有破折号但有附加参数?

[英]optional python arguments without dashes but with additional parameters?

what I'd like to do in Python is accept arguments of the following format: 我想在Python中做的是接受以下格式的参数:

script.py START | STOP | STATUS | MOVEABS <x> <y> | MOVEREL <x> <y>

So in other words, 换句话说,

  1. I don't want to deal with hyphens; 我不想处理连字符;
  2. I have multiple possibilities, ONE of which is required; 我有多种可能性,其中一种是必需的;
  3. Each is mutually exclusive; 每个都是互相排斥的;
  4. Some of the commands (EG moveabs and moverel) have additional required arguments, but these args and should not be present with any other argument. 一些命令(EG moveabs和moverel)具有额外的必需参数,但这些参数不应与任何其他参数一起出现。

Can this be done in python and would I use argparse or something else? 这可以在python中完成,我会使用argparse还是别的? Thanks. 谢谢。

Using docopt you can do this quite easily. 使用docopt您可以非常轻松地完成此操作。

Install docopt first: 首先安装docopt

$ pip install docopt

Write the script.py : 编写script.py

"""
Usage:
    script.py (start | stop | status | moveabs <x> <y> | moverel <x> <y>)
"""
from docopt import docopt

if __name__ == "__main__":
    args = docopt(__doc__)
    print args

and run it: 并运行它:

first showing basic help: 首先显示基本帮助:

$ python script.py
Usage:
    script.py (start | stop | status | moveabs <x> <y> | moverel <x> <y>)

then try subcommands: 然后尝试子命令:

start 开始

$ python script.py start
{'<x>': None,
 '<y>': None,
 'moveabs': False,
 'moverel': False,
 'start': True,
 'status': False,
 'stop': False}

stop

$ python script.py stop
{'<x>': None,
 '<y>': None,
 'moveabs': False,
 'moverel': False,
 'start': False,
 'status': False,
 'stop': True}

moveabs moveabs

$ python script.py moveabs 11 22
{'<x>': '11',
 '<y>': '22',
 'moveabs': True,
 'moverel': False,
 'start': False,
 'status': False,
 'stop': False}

moverel moverel

$ python script.py moverel 11 22
{'<x>': '11',
 '<y>': '22',
 'moveabs': False,
 'moverel': True,
 'start': False,
 'status': False,
 'stop': False}

add_parser with subparsers would do the trick add_parser和subparsers可以解决问题

import argparse
parser = argparse.ArgumentParser(prog='script.py')
sp = parser.add_subparsers(dest='cmd')
for cmd in ['START', 'STOP', 'STATUS']:
    sp.add_parser(cmd)
for cmd in ['MOVEABS', 'MOVEREL']:
    spp = sp.add_parser(cmd)
    spp.add_argument('x', type=float)
    spp.add_argument('y', type=float)
parser.print_help()
args = parser.parse_args()
print(args)

producing the likes of: 产生如下:

2137:~/mypy$ python2.7 stack23304740.py MOVEREL -h
usage: script.py [-h] {START,STOP,STATUS,MOVEABS,MOVEREL} ...

positional arguments:
  {START,STOP,STATUS,MOVEABS,MOVEREL}

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

usage: script.py MOVEREL [-h] x y

positional arguments:
  x
  y

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

and

2146:~/mypy$ python2.7 stack23304740.py MOVEREL 1.0 2.0
...
Namespace(cmd='MOVEREL', x=1.0, y=2.0)

and

2147:~/mypy$ python2.7 stack23304740.py START
...
Namespace(cmd='START')

The MOVEREL arguments could be named <x> and <y> , but then you'd have to access them via args['<y>'] instead of args.y . MOVEREL参数可以命名为<x><y> ,但是你必须通过args['<y>']而不是args.y来访问它们。 metavar='<x>' could be used to change the display but not the Namespace name. metavar='<x>'可用于更改显示但不能更改命名空间名称。

You could also use spp.add_argument('point', nargs=2, type=float) . 你也可以使用spp.add_argument('point', nargs=2, type=float) Unfortunately there's a bug that keeps us from using a metavar in this nargs=2 case, http://bugs.python.org/issue14074 . 不幸的是,有一个错误阻止我们在这个nargs=2案例中使用metavar, http: nargs=2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM