python::argparse: 如何逐级打印帮助?

python::argparse: How to print help level-by-level?

我有一个像'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)
    ...

在您的示例中,调用 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