如何使python argparse没有前缀的互斥组参数? [英] How to make python argparse mutually exclusive group arguments without prefix?
问题描述
Python2.7 argparse 只接受互斥组中的可选参数(带前缀):
Python2.7 argparse only accepts optional arguments (prefixed) in mutually exclusive groups:
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
$ 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
有没有办法让 argparse 参数表现得像传统的 unix 守护进程控制:
Is there a way to make argparse arguments behaves like traditional unix daemon control:
(start | stop | restart) and not (--start | --stop | --restart) ?
推荐答案
对于 argparse
中的所有功能和选项,我认为您永远不会得到看起来像的罐头"用法字符串喜欢你想要的.
For all the abilities and options in argparse
I don't think you'll ever get a "canned" usage string that looks like what you want.
也就是说,自从您发布原始帖子以来,您是否查看过子解析器?
That said, have you looked at sub-parsers since your original post?
这是一个准系统实现:
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()
使用 -h
选项运行它会产生:
Running this with the -h
option yields:
usage: mydaemon [-h] {start,stop,restart} ...
positional arguments:
{start,stop,restart}
start Starts mydaemon daemon
stop Stops mydaemon daemon
restart Restarts mydaemon daemon
这种方法的好处之一是能够为每个子解析器使用 set_defaults
将函数直接连接到参数.我还为 stop
和 restart
添加了优雅"选项:
One of the benefits of this approach is being able to use set_defaults
for each sub-parser to hook up a function directly to the argument. I've also added a "graceful" option for stop
and 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')
# Hook subparsers up to functions
sp_stop.set_defaults(func=my_stop)
# Uncomment when my_start() and
# my_restart() are implemented
#
# sp_start.set_defaults(func=my_start)
# sp_restart.set_defaults(func=my_restart)
args = parser.parse_args()
args.func(args)
显示 stop
的帮助"信息:
Showing the "help" message for 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
优雅地"停止:
$ python mydaemon.py stop -g
Let's try to stop...
这篇关于如何使python argparse没有前缀的互斥组参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!