gpt4 book ai didi

python - 与所有开关一起编程,运行良好,但 argparse '--help' 抛出很多错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:17 25 4
gpt4 key购买 nike

<分区>

我在 Python 中使用 argparse 来处理程序中的参数。例如,如下所示,如果我使用参数“-p”,则会执行一个特定的模块。现在,所有参数和程序运行良好。但是,当我尝试为我的程序获取“--help”时,它崩溃得很厉害(图片)。

这是与 argparse 有关的代码:

parser = argparse.ArgumentParser(description="something1.")
parser.add_argument('-x', '--xoo', help='something2', action='store_true')
parser.add_argument('-a', '--al', help='something3', action='store_true')
parser.add_argument('-c', '--conv', help='something4', type=float)
parser.add_argument('-p', '--pay', help='something5', type=float)
args = parser.parse_args()

关于如何更正此错误的任何想法?

$ ./xoom.py -h
Traceback (most recent call last):
File "./xoom.py", line 46, in <module>
args = parser.parse_args()
File "/usr/lib/python2.7/argparse.py", line 1688, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python2.7/argparse.py", line 1723, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/usr/lib/python2.7/argparse.py", line 1929, in _parse_known_args
start_index = consume_optional(start_index)
File "/usr/lib/python2.7/argparse.py", line 1869, in consume_optional
take_action(action, args, option_string)
File "/usr/lib/python2.7/argparse.py", line 1797, in take_action
action(self, namespace, argument_values, option_string)
File "/usr/lib/python2.7/argparse.py", line 994, in __call__
parser.print_help()
File "/usr/lib/python2.7/argparse.py", line 2319, in print_help
self._print_message(self.format_help(), file)
File "/usr/lib/python2.7/argparse.py", line 2293, in format_help
return formatter.format_help()
File "/usr/lib/python2.7/argparse.py", line 279, in format_help
help = self._root_section.format_help()
File "/usr/lib/python2.7/argparse.py", line 209, in format_help
func(*args)
File "/usr/lib/python2.7/argparse.py", line 209, in format_help
func(*args)
File "/usr/lib/python2.7/argparse.py", line 515, in _format_action
help_text = self._expand_help(action)
File "/usr/lib/python2.7/argparse.py", line 601, in _expand_help
return self._get_help_string(action) % params
TypeError: float argument required, not dict

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