【问题标题】:Argparse subcommand order in help output帮助输出中的 argparse 子命令顺序
【发布时间】:2013-03-08 03:42:58
【问题描述】:

使用 Python 的 argparse 模块,有没有办法在帮助输出中对通过使用子解析器创建的子命令进行排序?

【问题讨论】:

标签: python argparse


【解决方案1】:

我实际上找到了一种使用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()

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 2016-09-18
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2021-09-16
    • 2020-10-12
    • 2012-08-29
    • 2021-12-06
    相关资源
    最近更新 更多