argparse subparser monolithic help output

后端 未结 5 555
予麋鹿
予麋鹿 2020-11-29 06:02

My argparse has only 3 flags (store_true) on the top level, everything else is handled through subparsers. When I run myprog.py --help, the output shows a list

5条回答
  •  猫巷女王i
    2020-11-29 06:37

    I was also able to print a short help for commands using _choices_actions.

    def print_help(parser):
      print(parser.description)
      print('\ncommands:\n')
    
      # 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))
    

提交回复
热议问题