Python-Argparse:调用help参数返回错误

时间:2019-01-23 11:48:47

标签: python argparse

代码如下: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!@

问题是默认的“帮助”参数(-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),看看我是否能更好地了解正在发生的事情(没有编辑任何内容),但我却不明白(这里是初学者)。预先感谢。

1 个答案:

答案 0 :(得分:3)

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

  

4-仅!@#$%¨&*

如果要打印%,请使用%%代替%

像这样

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

这可能是argparse似乎在% self._get_help_string(action)参数中使用了%格式,否则不必%进行转义。

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