1

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

4

1 回答 1

2

我实际上找到了一种使用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()
于 2013-03-19T16:48:11.567 回答