[英]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, 换句话说,
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
您可以非常轻松地完成此操作。
docopt
first: docopt
: $ pip install docopt
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
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.