argparse subparser单片帮助输出

时间:2013-11-20 11:06:05

标签: python argparse

我的argparse在顶层只有3个标志(store_true),其他一切都是通过subparsers处理的。当我运行myprog.py --help时,输出显示所有子命令的列表,如normal,{sub1, sub2, sub3, sub4, ...}。所以,默认设置很好......

我通常记不起我需要的确切子命令名称及其所有选项。所以我最终做了两次帮助查找:

myprog.py --help
myprog.py sub1 --help

我经常这样做,我决定把它塞进一步。我宁愿让我的顶级帮助输出一个巨大的摘要,然后我手动滚动列表。我发现它要快得多(至少对我而言)。

我正在使用RawDescriptionHelpFormatter,并手动输入长帮助输出。但现在我有很多子命令,它变得很难管理。

有没有办法通过一次程序调用获得详细的帮助输出?

如果没有,我如何迭代我的argparse实例的子分析器,然后从每个实例中单独检索帮助输出(之后我将粘合在一起)?


这是我的argparse设置的快速概述。我清理/删除了相当多的代码,所以如果没有一些帮助,这可能无法运行。

parser = argparse.ArgumentParser(
        prog='myprog.py',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent(""" You can manually type Help here """) )

parser.add_argument('--debuglog', action='store_true', help='Verbose logging for debug purposes.')
parser.add_argument('--ipyonexit', action='store_true', help='Drop into an embeded Ipython session instead of exiting command.')

subparser = parser.add_subparsers()

### --- Subparser B
parser_b = subparser.add_parser('pdfreport', description="Used to output reports in PDF format.")
parser_b.add_argument('type', type=str, choices=['flatlist', 'nested', 'custom'],
                        help="The type of PDF report to generate.")
parser_b.add_argument('--of', type=str, default='',
                        help="Override the path/name of the output file.")
parser_b.add_argument('--pagesize', type=str, choices=['letter', '3x5', '5x7'], default='letter',
                        help="Override page size in output PDF.")
parser_b.set_defaults(func=cmd_pdf_report)

### ---- Subparser C
parser_c = subparser.add_parser('dbtables', description="Used to perform direct DB import/export using XLS files.")
parser_c.add_argument('action', type=str, choices=['push', 'pull', 'append', 'update'],
                        help="The action to perform on the Database Tables.")
parser_c.add_argument('tablename', nargs="+",
                        help="The name(s) of the DB-Table to operate on.")
parser_c.set_defaults(func=cmd_db_tables)

args = parser.parse_args()
args.func(args)

7 个答案:

答案 0 :(得分:14)

这有点棘手,因为argparse没有直接公开已定义的子解析器列表。但它可以做到:

import argparse

# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--foo', action='store_true', help='foo help')
subparsers = parser.add_subparsers(help='sub-command help')

# create the parser for the "a" command
parser_a = subparsers.add_parser('a', help='a help')
parser_a.add_argument('bar', type=int, help='bar help')

# create the parser for the "b" command
parser_b = subparsers.add_parser('b', help='b help')
parser_b.add_argument('--baz', choices='XYZ', help='baz help')
# print main help
print(parser.format_help())

# 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, subparser in subparsers_action.choices.items():
        print("Subparser '{}'".format(choice))
        print(subparser.format_help())

此示例适用于python 2.7和python 3.示例解析器来自Python 2.7 documentation on argparse sub-commands

唯一要做的就是为完整的帮助添加新参数,或者替换内置的-h/--help

答案 1 :(得分:11)

以下是使用自定义帮助处理程序的完整解决方案(几乎所有来自@Adaephon的代码都回答):

import argparse


class _HelpAction(argparse._HelpAction):

    def __call__(self, parser, namespace, values, option_string=None):
        parser.print_help()

        # 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, subparser in subparsers_action.choices.items():
                print("Subparser '{}'".format(choice))
                print(subparser.format_help())

        parser.exit()

# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG', add_help=False)  # here we turn off default help action

parser.add_argument('--help', action=_HelpAction, help='help for help if you need some help')  # add custom help

parser.add_argument('--foo', action='store_true', help='foo help')
subparsers = parser.add_subparsers(help='sub-command help')

# create the parser for the "a" command
parser_a = subparsers.add_parser('a', help='a help')
parser_a.add_argument('bar', type=int, help='bar help')

# create the parser for the "b" command
parser_b = subparsers.add_parser('b', help='b help')
parser_b.add_argument('--baz', choices='XYZ', help='baz help')

parsed_args = parser.parse_args()

答案 2 :(得分:3)

在Adaephon的例子中迭代子分析器的一种更简单的方法是

for subparser in [parser_a, parser_b]:
   subparser.format_help()

Python允许您访问隐藏的属性,如parser._actions,但不鼓励这样做。在定义解析器时,构建自己的列表同样容易。使用参数做特殊事情同样如此。由于某种原因,add_argumentadd_subparser会返回各自的ActionParser个对象。

如果我正在制作ArgumentParser的子类,我可以随意使用_actions。但对于一次性申请,建立我自己的清单会更清楚。


一个例子:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('mainpos')
parser.add_argument('--mainopt')
sp = parser.add_subparsers()
splist = []   # list to collect subparsers
sp1 = sp.add_parser('cmd1')
splist.append(sp1)
sp1.add_argument('--sp1opt')
sp2 = sp.add_parser('cmd2')
splist.append(sp2)
sp2.add_argument('--sp2opt')

# collect and display for helps    
helps = []
helps.append(parser.format_help())
for p in splist:
   helps.append(p.format_help())
print('\n'.join(helps))

# or to show just the usage
helps = []
helps.append(parser.format_usage())
for p in splist:
   helps.append(p.format_usage())
print(''.join(helps))

组合的“用法”显示为:

usage: stack32607706.py [-h] [--mainopt MAINOPT] mainpos {cmd1,cmd2} ...
usage: stack32607706.py mainpos cmd1 [-h] [--sp1opt SP1OPT]
usage: stack32607706.py mainpos cmd2 [-h] [--sp2opt SP2OPT]

组合帮助的显示是漫长且多余的。可以在格式化之后或使用特殊帮助格式化程序以各种方式进行编辑。但是谁会做出这样的选择?

答案 3 :(得分:0)

我还可以使用_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))

答案 4 :(得分:0)

也许更简单的方法是使用parser.epilog

def define_parser():
    import argparse
    parser = argparse.ArgumentParser(
        prog='main',
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    commands = parser.add_subparsers(
        title="required commands",
        help='Select one of:',
    )    
    command_list = commands.add_parser(
        'list',
        help='List included services',
    )
    command_ensure = commands.add_parser(
        'ensure',
        help='Provision included service',
    )
    command_ensure.add_argument(
        "service",
        help='Service name',
    )
    import textwrap
    parser.epilog = textwrap.dedent(
        f"""\
        commands usage:\n
        {command_list.format_usage()}
        {command_ensure.format_usage()}
        """
    )
    return parser

parser = define_parser()

parser.print_help()

这将导致以下输出:

usage: main [-h] {list,ensure} ...

optional arguments:
  -h, --help     show this help message and exit

required commands:
  {list,ensure}  Select one of:
    list         List included services
    ensure       Provision included service

commands usage:

usage: main list [-h]

usage: main ensure [-h] service

答案 5 :(得分:0)

add_subparsers().add_parser() 不仅接受出现在子命令帮助中的 description,还接受在顶级解析器中用作单行描述的 help= '帮助。

docs 将其隐藏在公式中

<块引用>

(但是,可以通过将 help= 参数提供给上述 add_parser() 来给出每个子解析器命令的帮助消息。)

甚至在这句话周围的示例代码中:

<块引用>
>>> # create the parser for the "b" command
>>> parser_b = subparsers.add_parser('b', help='b help')
>>> parser_b.add_argument('--baz', choices='XYZ', help='baz help')

[...]

usage: PROG [-h] [--foo] {a,b} ...

positional arguments:
  {a,b}   sub-command help
    a     a help
    b     b help

是的,这不是所有事情的全部帮助,但恕我直言,它很好地涵盖了基本用例,并且不容易发现。

答案 6 :(得分:-2)

我有一些简单的包装器,它们按顺序存储各种引用(Parser,SubParser,StoreAction),以便在帮助生成期间轻松迭代。

我现在正在组织,详细,自动生成帮助输出。当我有机会时,我会发布概述。

有一个缺点,这与在Optional Arguments上生成的帮助内容有关:它不是很好。改善这些帮助输出将不仅仅是一个包装器(如果我们想要保持干净)。但是如果你想为不断发展的程序提供一个很好的帮助概述,那么这应该满足大多数。

相关问题