1

Using Python's argparse module, is there a way to order sub-commands created through the use of subparsers in the help output?

jd.
  • 10,678
  • 3
  • 46
  • 55

1 Answers1

2

I actually found a way using 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()
jd.
  • 10,678
  • 3
  • 46
  • 55