我一直在用Argparse编写命令行程序已经有一段时间了,我试图用这样的方式编写它,当用户将以下内容提供给命令行时:
$python my_script.py -h
将打印出帮助部分(用法),打印出主解析器的帮助部分,以及子分析器的简要概述。
但是现在,无论何时我输入上一行到我的终端,我都没有收到任何用法,而是获得了大量的追溯和以下错误:
TypeError: expected string or buffer
在使用基于argparse的命令行程序之前,我从未发生过这个错误。此外,如果我提供其中一个子分析器的名称,
$python my_script.py subparserA -h
我打印出subparser的用法。其他子分析师也是如此。
那么为什么我不可能获得主解析器的用法?这之前对我有用,所以我不知道为什么它现在不起作用。我真的希望用户能够查看可用的不同子分析器的概述。
我的基本代码目前按以下方式设置:
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
答案 0 :(得分:0)
您应该使用
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
来电。
我无法重现你的
大规模追溯和以下错误: TypeError:期望的字符串或缓冲区
我需要查看回溯(或其中的一部分)以查看究竟是什么引发了错误。这不是正常的argparse
错误;当然不是argparse
陷阱和重新路由的人。
有关How to Set a Default Subparser using Argparse Module with Python 2.7
所需/不需要的子分析器行为的更多信息答案 1 :(得分:0)
对于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"))