使用Python的argparse
模块,有没有办法在帮助输出中对通过使用子解析器创建的子命令进行排序?
最佳答案
我实际上找到了一种使用argparse.HelpFormatter
的方法。
class CustomHelpFormatter(argparse.HelpFormatter):
def _iter_indented_subactions(self, action):
try:
get_subactions = action._get_subactions
except AttributeError:
pass
else:
self._indent()
if isinstance(action, argparse._SubParsersAction):
for subaction in sorted(get_subactions(), key=lambda x: x.dest):
yield subaction
else:
for subaction in get_subactions():
yield subaction
self._dedent()
关于python - 帮助输出中的 Argparse 子命令顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15499955/