python - 使用 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
解决方案
只需从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
推荐阅读
- javascript - 在引导程序 4 行和列中对齐 fetch api 对象数组,使用 foreach 循环遍历数组
- react-native - React Native 平面列表 || 当在 TextInput 字段中按下时,VirtualizedList 从 0 个索引重新开始
- python - 使用 python 请求和可能的 concurrent.futures 来处理实时数据
- vuetify.js - 设置要在 Vuetify 主题颜色上使用的字体颜色
- rust - 如何使用 cpuid 找出 CPU 内核的数量?
- mongodb - 我可以在两个不同的集合中使用相同的 id 吗?
- backup - 如何实现快照复制
- python - 根据先前的值添加到列表
- r - 仅部分转置以匹配两个特定变量为条件的数据帧的某些元素
- javascript - 如果 useState 不更改或保持最后一个值,如何在 react 中重新渲染组件