gpt4 book ai didi

Python-Argparse : Calling the help argument returns an error

转载 作者:行者123 更新时间:2023-11-30 22:00:26 25 4
gpt4 key购买 nike

代码如下:https://github.com/zSucrilhos/programming/blob/master/Python/psw-generator-ASCII-1.9.5-CLI-t7.py

在 Repl.it 上:https://repl.it/@ErickCesar/PushyFabulousTask

这是一个密码生成器,我做这个是为了好玩,主要是为了学习 Python。它工作正常,正如预期的那样,我设置了以下参数:

-np, --repeat = Generate more than one psw at a time (default=1)
-pl, --length = Password length (default=25 chars)
-pt, --type = Password's type; Can be one of the following:
                             1 - UPPERCASE ONLY
2 - lowercase only
3 - 1234567890 only
4 - !@#$%¨&* only
5 - Mixed 12ab!@

问题是默认的“help”参数(-h、--help)。当我尝试运行该程序时,它显示一条大错误消息:

C:\Users\Pentium IV 641\Desktop\programming\programming\Python>python psw-generator-ASCII-1.9.5-CLI-t7.py -h
Traceback (most recent call last):
File "psw-generator-ASCII-1.9.5-CLI-t7.py", line 118, in <module>
arguments = parser.parse_args()
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 1730, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 1762, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 1968, in _parse_known_args
start_index = consume_optional(start_index)
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 1908, in consume_optional
take_action(action, args, option_string)
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 1836, in take_action
action(self, namespace, argument_values, option_string)
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 1020, in __call__
parser.print_help()
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 2362, in print_help
self._print_message(self.format_help(), file)
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 2346, in format_help
return formatter.format_help()
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 282, in format_help
help = self._root_section.format_help()
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 213, in format_help
item_help = join([func(*args) for func, args in self.items])
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 213, in <listcomp>
item_help = join([func(*args) for func, args in self.items])
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 213, in format_help
item_help = join([func(*args) for func, args in self.items])
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 213, in <listcomp>
item_help = join([func(*args) for func, args in self.items])
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 519, in _format_action
help_text = self._expand_help(action)
File "C:\Users\Pentium IV 641\AppData\Local\Programs\Python\Python36-32\lib\argparse.py", line 606, in _expand_help
return self._get_help_string(action) % params
ValueError: unsupported format character '?' (0xa8) at index 154

尝试添加我自己的 -h--help 参数,但没有帮助。错误将以同样的方式显示。

在 Linux (Arch) 和 Windows 10 中都尝试过,同样的事情。

我不知道接下来要尝试什么,因为我不太理解错误,无法自己解决它。所以我寻求帮助。

我还查看了库文件(argparse.py),看看我是否可以更好地理解正在发生的事情(没有编辑任何内容),但我不能(这里是初学者)。提前致谢。

最佳答案

问题出在字符串中的这个字符 (%)。

4 - !@#$%¨&* only

如果您想打印 %,请使用 %% 而不是 %

像这样。

4 - !@#$%%¨&* only

This could be argparse seems to use % formatting in self._get_help_string(action) % params, otherwise % does not have to be escaped.

这是一个类似的相关问题Python string formatting when string contains “%s” without escaping

关于Python-Argparse : Calling the help argument returns an error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326603/

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