如何打印 Argparse 主解析器的帮助(用法)部分而不是子解析器的用法?

How to print the help (usage) section for the main parser of Argparse and not the usage of a subparser?

我用 Argparse 编写命令行程序已经有一段时间了,我试图以这样的方式编写它,即当用户向命令行提供以下内容时:

$python my_script.py -h

将打印出一个帮助部分(用法),打印出主解析器的帮助部分,以及子解析器的简要概述。

但是现在,每当我在终端中输入上一行时,我都没有收到任何使用信息,而是收到大量回溯和以下错误:

TypeError: expected string or buffer

在使用基于 argparse 的命令行程序之前,我从未遇到过这个错误。此外,如果我提供其中一个子解析器的名称,

$python my_script.py subparserA -h

我得到了子解析器用法的打印输出。其他子解析器也是如此。

那么为什么我无法获取主解析器的用法?这以前对我有用,所以我不知道为什么现在不起作用。我真的希望用户能够查看可用的不同子解析器的概述。

我的基本代码目前设置如下:

import argparse
import sys

if __name__ == "__main__":
    Parser = argparse.ArgumentParser(prog= "My_program")

    Parser.description= "This program does A and B things."
    subparsers= Parser.add_subparsers(help= "SubparserA does A things and SubparserB does B things", dest='mode')

    subparserA= subparsers.add_parser("subparserA", help= "Additional explanation of what A things entail")

    subparserA.add_arguments("-foo", required=True, help= "foo is needed for SubparserA to work")

    subparserB= subparsers.add_parser("subparserB", help="Additional explanation of what B things entail")

    subparserB.add_argument("-bar", required=True, help= "bar is needed for SubparserB to work")

    args= Parser.parse_args()

    if args.mode == "subparserA":
        ###do things pertinent to subparserA
    elif args.mode== "subparserB":
        ###do things pertinent to subparserB
    else:
        argparse.print_help()
        argparse.ArgumentError("too few arguments")

更新

这是错误的完整回溯:

Traceback (most recent call last):
  File "my_program.py", line 164, in <module>
    args= Parser.parse_args()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1701, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1733, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1939, in _parse_known_args
    start_index = consume_optional(start_index)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1879, in consume_optional
    take_action(action, args, option_string)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1807, in take_action
    action(self, namespace, argument_values, option_string)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 996, in __call__
    parser.print_help()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 2340, in print_help
    self._print_message(self.format_help(), file)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 2314, in format_help
    return formatter.format_help()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 281, in format_help
    help = self._root_section.format_help()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 211, in format_help
    func(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 485, in _format_text
    return self._fill_text(text, text_width, indent) + '\n\n'
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 621, in _fill_text
    text = self._whitespace_matcher.sub(' ', text).strip()
TypeError: expected string or buffer

你应该使用

Parser.print_help()
Parser.error('too few arguments')

即使用现有Parser对象的方法。


当我运行你的脚本

1019:~/mypy$ python stack46754855.py 
Traceback (most recent call last):
  File "stack46754855.py", line 10, in <module>
    subparserA= subparsers.add_parser("subparserA", help= "Additional explanation of what A things entail", dest= 'mode')
  File "/usr/lib/python2.7/argparse.py", line 1066, in add_parser
    parser = self._parser_class(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'dest'

dest 不是 add_parser 方法的有效参数。它是 add_subparsers.

的有效且有用的参数
subparsers= Parser.add_subparsers(dest='mode')

它也反对add_arguments方法。

更正后我得到:

1022:~/mypy$ python stack46754855.py 
usage: My_program [-h] {subparserA,subparserB} ...
My_program: error: too few arguments

在 Py2 中,subparsers 是必需的参数。它在 Py3 中是可选的(一个错误),允许脚本 运行 无效的 argparse.print_help 调用:

1022:~/mypy$ python3 stack46754855.py 
Traceback (most recent call last):
  File "stack46754855.py", line 27, in <module>
    argparse.print_help()
AttributeError: module 'argparse' has no attribute 'print_help'

根据我上面建议的更改:

1025:~/mypy$ python3 stack46754855.py 
usage: My_program [-h] {subparserA,subparserB} ...

This program does A and B things.

positional arguments:
  {subparserA,subparserB}
                        SubparserA does A things and SubparserB does B things
    subparserA          Additional explanation of what A things entail
    subparserB          Additional explanation of what B things entail

optional arguments:
  -h, --help            show this help message and exit
usage: My_program [-h] {subparserA,subparserB} ...
My_program: error: too few arguments

第二个 usage 来自 Parser.error 调用。


我无法复制你的

massive traceback and the following error: TypeError: expected string or buffer

我需要查看回溯(或部分回溯)以了解究竟是什么引发了错误。这不是正常的 argparse 错误;当然这不是 argparse 陷阱和改道。


更多关于 required/not 所需的子解析器行为在

对于 parser.add_argument 中的多行帮助字符串,使用 + 而不是 ,。如果您使用 ',' 在多行中拆分了参数帮助,那么您将看到此问题

parser.add_argument("xml",help=("long help here",
                                " long help second line"))

这将导致上述异常

改为

parser.add_argument("xml",help=("long help here" +
                                " long help second line"))