首页 > 解决方案 > 使用 argparse 从 --help 标志中获取帮助菜单的某个部分

问题描述

--help在我的一个程序中传递时,我有一个非常长的帮助菜单:

optional arguments:
  -h, --help            show this help message and exit

mandatory arguments:
  arguments that have to be passed for the program to run

  -u URL, --url URL     Pass a single URL to detect the protection
...
request arguments:
  arguments that will control your requests

  --pa USER-AGENT       Provide your own personal agent to use it for the HTTP
...
encoding options:
  arguments that control the encoding of payloads

  -e PAYLOAD [TAMPER-SCRIPT-LOAD-PATH ...], --encode PAYLOAD [TAMPER-SCRIPT-LOAD-PATH ...]
...
output options:
  arguments that control how WhatWaf handles output

  -F, --format          Format the output into a dict and display it
...
misc arguments:
  arguments that don't fit in any other category

  --verbose             Run in verbose mode (more output)
...

构建参数的结构如下所示:

        mandatory = parser.add_argument_group("mandatory arguments",
                                              "arguments that have to be passed for the program to run")
        mandatory.add_argument("-u", "--url", dest="runSingleWebsite", metavar="URL",
                               help="Pass a single URL to detect the protection")
...
        req_args = parser.add_argument_group("request arguments",
                                             "arguments that will control your requests")
        req_args.add_argument("--pa", dest="usePersonalAgent", metavar="USER-AGENT",
                              help="Provide your own personal agent to use it for the HTTP requests")
...
        encoding_opts = parser.add_argument_group("encoding options",
                                                  "arguments that control the encoding of payloads")
        encoding_opts.add_argument("-e", "--encode", dest="encodePayload", nargs="+", metavar=("PAYLOAD", "TAMPER-SCRIPT-LOAD-PATH"),
                                   help="Encode a provided payload using provided tamper script(s) "
                                        "you are able to payy multiple tamper script load paths to "
                                        "this argument and the payload will be tampered as requested")
...
... # so on so forth

我想要发生的是获取单个参数组帮助消息,例如encoding options显示该组帮助。有没有内置的方法可以做到这一点,或者将完整的帮助菜单写入文件并从那里解析它是最简单的解决方案?

IE:

def gen_menu(data):
    with open(some_temp_file,"a+") as f:
        f.write(data)
    search_through_data_for_encoding
    return to_the_end_of_encoding

标签: pythonpython-3.xpython-2.7

解决方案


只需从encoding_opts操作组中获取该信息。帮助格式化程序从组中获取.title.description属性,然后是组的参数(通过._group_actions属性)。

API的argparse HelpFormatter文档严重不足,我们必须依靠私有实现细节来获取组的参数,但您可以使用配置的帮助格式化程序重新创建字符串:

def help_for_group(parser, group):
    formatter = parser.formatter_class(None)  # required argument, but None suffices.
    formatter.start_section(group.title)
    formatter.add_text(group.description)
    formatter.add_arguments(group._group_actions)
    formatter.end_section()
    return formatter.format_help()

print(help_for_group(parser, encoding_opts))

使用您的示例进行演示encoding_opts

>>> print(help_for_group(parser, encoding_opts))
encoding options:
  arguments that control the encoding of payloads

  -e PAYLOAD [TAMPER-SCRIPT-LOAD-PATH ...], --encode PAYLOAD [TAMPER-SCRIPT-LOAD-PATH ...]
                        Encode a provided payload using provided tamper
                        script(s) you are able to payy multiple tamper script
                        load paths to this argument and the payload will be
                        tampered as requested

推荐阅读