我有一個像“git”這樣的多級命令列程式。
my_cli service action --options
我想逐級顯示幫助訊息,并且我不希望用戶明確鍵入“-h”或“--help”。
例如,
$ my_cli <== display help of all services
$ my_cli service1 <== display help for service1 only
$ my_cli service1 action1 <== display help for service1/action1 only
代碼如下所示。
import argparse
argument_parser = argparse.ArgumentParser(description="my_cli")
root_parsers = argument_parser.add_subparsers(title="service", dest="service")
service1_parsers = root_parsers.add_parser("service1", help="service1").add_subparsers(title="action", dest="action")
service2_parsers = root_parsers.add_parser("service2", help="service2").add_subparsers(title="action", dest="action")
service1_action1_parser = service1_parsers.add_parser("action1", help="action1")
service1_action1_parser.add_argument("-a", "--address", required=True, help="address or hostname of the server")
...
args = argument_parser.parse_args()
if (args.service is None):
argument_parser.print_help()
exit(1)
elif (args.action is None):
if (args.service == "service1"):
service1_parsers.print_help() <== This doesn't work.
exit(1)
...
else:
if (args.service == "service1") AND (args.action == "action1"):
service1_action1_parser.print_help() <== This doesn't work.
exit(1)
...
uj5u.com熱心網友回復:
在您的示例中,呼叫my_cli service1 action1 確實顯示了某種幫助訊息,但它更像是一條使用訊息,因為您已將--address引數標記為必需,這導致決議器驗證失敗。使用訊息是
usage: test3.py service1 action1 [-h] -a ADDRESS
test3.py service1 action1: error: the following arguments are required: -a/--address
您的呼叫示例中my_cli service1未顯示幫助訊息的問題是,print_help()當您應該在決議器上呼叫它時,您正在呼叫它。像這樣的事情應該有效。
import argparse
argument_parser = argparse.ArgumentParser(description="my_cli")
root_parsers = argument_parser.add_subparsers(title="service", dest="service")
service1_parser = root_parsers.add_parser("service1", help="service1")
service1_subparsers = service1_parser.add_subparsers(title="action", dest="action")
service2_parser = root_parsers.add_parser("service2", help="service2")
service2_subparsers = service2_parser.add_subparsers(title="action", dest="action")
service1_action1_parser = service1_subparsers.add_parser("action1", help="action1")
# I removed the required=True here for the purposes of showing how to get the help message
service1_action1_parser.add_argument("-a", "--address", help="address or hostname of the server")
args = argument_parser.parse_args()
if args.service is None:
argument_parser.print_help()
exit(1)
elif args.action is None:
if args.service == "service1":
# call print_help() on a parser instead of a subparser
service1_parser.print_help()
elif args.service == "service2":
service2_parser.print_help()
exit(1)
elif args.service == "service1" and args.action == "action1":
service1_action1_parser.print_help()
exit(1)
我得到的輸出是:
$ ./my_cli
usage: my_cli [-h] {service1,service2} ...
my_cli
optional arguments:
-h, --help show this help message and exit
service:
{service1,service2}
service1 service1
service2 service2
$ ./my_cli service1
usage: my_cli service1 [-h] {action1} ...
optional arguments:
-h, --help show this help message and exit
action:
{action1}
action1 action1
$ ./my_cli service1 action1
usage: my_cli service1 action1 [-h] [-a ADDRESS]
optional arguments:
-h, --help show this help message and exit
-a ADDRESS, --address ADDRESS
address or hostname of the server
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/367730.html
上一篇:Python.update()帶有更新數量的字典購物車串列
下一篇:重組元組串列
