简体   繁体   中英

Python - Argparse: Calling the help argument returns an error

Here's the code: https://github.com/zSucrilhos/programming/blob/master/Python/psw-generator-ASCII-1.9.5-CLI-t7.py

And on Repl.it: https://repl.it/@ErickCesar/PushyFabulousTask

This is a password generator that i'm doing for fun and mostly to learn Python. It works fine, as expected, i set the following arguments:

-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!@

The problem is the default "help" argument (-h, --help). It shows an big error message when i try to run the program:

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

Tried adding the -h and --help argument by my own and it didn't helped. The error would show the same way.

Tried both in Linux (Arch) and Windows 10, same thing.

I don't know exactly what to try next, because i don't understood the error well to solve it by my own. So i'm asking for help.

I also went to the library file (argparse.py) to see if i could understand better what was going on (didn't edited anything), but i couldn't (beginner here). Thanks in advance.

The problem was this character ( % ) in your string .

4 - !@#$%¨&* only

If you want to print % use %% instead of % .

like this.

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

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

This is a similar relevant question Python string formatting when string contains “%s” without escaping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM