0

Default argparse help output is ugly:

usage: gl.EXE [-h] [--version]
              {track,untrack,status,diff,commit,branch,tag,checkout,merge,resolve,fuse,remote,publish,switch,init,history}
              ...

Gitless - a version control system built on top of Git - http://gitless.com

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit

subcommands:
  {track,untrack,status,diff,commit,branch,tag,checkout,merge,resolve,fuse,remote,publish,switch,init,history}
    track               start tracking changes to files
    untrack             stop tracking changes to files
    status              show status of the repo
...

How can I format the output to be exactly like example below. With preserved order of commands

Gitless - a version control system built on top of Git - http://gitless.com

commands:

  track               start tracking changes to files
  untrack             stop tracking changes to files
  status              show status of the repo
...
anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140

1 Answers1

1

Subclassing argparse.RawDescriptionHelpFormatter was too much work, so I've used this hack to extract command list and build my own message.

def print_help(parser):
  """print help for humans"""
  print(parser.description)
  print('\ncommands:\n')

  # https://stackoverflow.com/questions/20094215/argparse-subparser-monolithic-help-output
  # retrieve subparsers from parser
  subparsers_actions = [
      action for action in parser._actions 
      if isinstance(action, argparse._SubParsersAction)]
  # there will probably only be one subparser_action,
  # but better save than sorry
  for subparsers_action in subparsers_actions:
      # get all subparsers and print help
      for choice in subparsers_action._choices_actions:
          print('    {:<19} {}'.format(choice.dest, choice.help))
anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
  • Yes, for that big of a change writing your own is often easier. Looks like you are making good use of the earlier discussion. For not-so-big changes writing a custom `parser.format_help` is another option. – hpaulj Mar 28 '17 at 20:34
  • @hpaulj, the earlier discussion helped a lot, but still it was quite a bit of time spent debugging argparse code to get to this snippet. Custom parser classes are huge and I prefer to keep CLI code compact if possible. – anatoly techtonik Apr 09 '17 at 06:43