我正在创建一个非常简单的脚本,它采用以下参数:
-p --port integer, optional, default 5050
-f --fork boolean, optional, default False
action string, required, needs to be either start or stop
我已经尝试在argparse中实现它,但是当没有提供动作字符串时它不会打印帮助,它只是失败了所有丑陋的样子:
usage: __init__.py [-h] [-p PORT] [-f] {start,stop}
__init__.py: error: argument action: invalid choice: '__init__.py' (choose from 'start', 'stop')
即使我通过“开始”或“停止”,它也会失败并显示相同的消息。这是我的代码:
parser = argparse.ArgumentParser(description="Start or stop the server.",
epilog="If you don't know what you're doing, run. Run for your life.\n")
parser.add_argument("-p", "--port", type=int, nargs=1, default=5050,
dest="port", help="The port to run the server on.")
parser.add_argument("-f", "--fork", action="store_true", default=False,
dest="fork", help="Fork to background? Default is false.")
parser.add_argument("action", type=str, choices=("start","stop"), help="Whether to 'start' or 'stop' the server.")
我在这里做错了什么?希望我的意图在我的代码中非常明确。
答案 0 :(得分:2)
当提供无效参数时,argparse module用于打印用法消息和问题描述然后退出,这正是您的示例中正在发生的事情。
如果您希望它打印帮助信息,您需要自己处理此案例。例如,如果未提供任何操作,此代码将打印帮助消息:
parser = argparse.ArgumentParser(description="Start or stop the server.",
epilog="If you don't know what you're doing, run. Run for your life.\n",
prog="myserver")
parser.add_argument("-p", "--port", type=int, nargs=1, default=5050,
dest="port", help="The port to run the server on.")
parser.add_argument("-f", "--fork", action="store_true", default=False,
dest="fork", help="Fork to background? Default is false.")
parser.add_argument("action", nargs="?", type=str, choices=("start","stop"),
help="Whether to 'start' or 'stop' the server.")
args = parser.parse_args()
if args.action is None:
parser.print_help()
sys.exit(1)
如果我在没有动作的情况下运行它,我会得到:
$ python s.py
usage: myserver [-h] [-p PORT] [-f] [{start,stop}]
Start or stop the server.
positional arguments:
{start,stop} Whether to 'start' or 'stop' the server.
optional arguments:
-h, --help show this help message and exit
-p PORT, --port PORT The port to run the server on.
-f, --fork Fork to background? Default is false.
If you don't know what you're doing, run. Run for your life.
但是,如果我使用无效操作或无效参数运行它,那么它将恢复到argparse模块的预期行为:
$ python s.py not_valid
usage: myserver [-h] [-p PORT] [-f] [{start,stop}]
myserver: error: argument action: invalid choice: 'not_valid' (choose from 'start', 'stop')
答案 1 :(得分:1)
您使用的是哪个版本的Python?当我用2.7.1运行你的代码时它工作正常。
$ ./t stop
$ ./t start
$ ./t -f start
$ ./t -f stop
$ ./t -f
usage: someprog [-h] [-p PORT] [-f] {start,stop}
someprog: error: too few arguments
$ ./t -f -p 8080
usage: someprog [-h] [-p PORT] [-f] {start,stop}
someprog: error: too few arguments
$ ./t -f -p 8080 start
一个提示,如果您在ctor中指定'prog',则可以使用 init .py作为文件名覆盖它
parser = argparse.ArgumentParser(
prog="someprog",
description="Start or stop the server.",
epilog="If you don't know what you're doing, run. Run for your life.\n"
)
此外,它是打印用法,但不是很长的帮助..你可以做这样的事情让事情变得更加明显......
try:
parser.parse_args()
except Exception e:
print "************************"
parser.print_help()
print "************************"