Python2.7argparse仅接受互斥组中的可选参数(前缀):
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
有没有一种方法可以让argparse参数的行为像传统的unix守护进程控制一样:
(start | stop | restart) and not (--start | --stop | --restart) ?
发布于 2011-12-24 08:00:44
对于argparse
中的所有功能和选项,我认为你永远不会得到一个看起来像你想要的“罐头”用法字符串。
也就是说,自从你最初的帖子以来,你有没有看过子解析器?
下面是一个基本的实现:
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
选项运行此命令会产生以下结果:
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
添加了一个“优雅”选项
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
的“帮助”消息
$ 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...
发布于 2011-10-24 05:44:24
听起来你想要一个位置参数而不是互斥的选项。您可以使用“选项”来限制可能的可接受选项。
parser = ArgumentParser()
parser.add_argument('action', choices=('start', 'stop', 'restart'))
这将生成一个使用行,如下所示:
usage: foo.py [-h] {start,stop,restart}
发布于 2012-09-21 20:51:35
基于亚当的回答。如果您想要指定一个默认值,您可以始终执行以下操作,以便他们可以有效地将其保留为空。
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())
它将打印:
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
https://stackoverflow.com/questions/7869345
复制相似问题