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

Posted

技术标签:

【中文标题】python::argparse:如何逐级打印帮助?【英文标题】:python::argparse: How to print help level-by-level? 【发布时间】:2022-01-04 21:05:51 【问题描述】:

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

【问题讨论】:

【参考方案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

【讨论】:

以上是关于python::argparse:如何逐级打印帮助?的主要内容,如果未能解决你的问题,请参考以下文章

Python argparse:有没有办法控制帮助文本的空白? [复制]

在没有任何参数的情况下调用脚本时使用 Python argparse 显示帮助消息

python argparse:如何在错误时自动显示帮助?

帮助文本中的 Python argparse 参数顺序

python argparse总结

带有编号参数名称的 Python argparse