gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:19:13 26 4
gpt4 key购买 nike

我用 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

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


我无法复制你的

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

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


How to Set a Default Subparser using Argparse Module with Python 2.7 了解更多关于必需/不需要的子解析器行为的信息

关于python-2.7 - 如何打印 Argparse 主解析器的帮助(用法)部分而不是子解析器的用法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46754855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com