1

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.

BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42
  • Not an answer, just a suggestion: Instead of allowing _only_ lowercase or _only_ uppercase or _only_ all mixed, how about using a bit-mask? 1 for lowercase, 2 for uppercase, 4 for numbers, 8 for special chars -> 9 for lowercase and special chars. – tobias_k Jan 23 '19 at 12:47
  • 1
    And **please** put your code into the body of the question (the argparse stuff starting in [`line 82`](https://github.com/zSucrilhos/programming/blob/master/Python/psw-generator-ASCII-1.9.5-CLI-t7.py#L82) should be sufficient). – shmee Jan 23 '19 at 12:53
  • tobias_k, like it. Thanks for the suggestion. – Erick César Jan 23 '19 at 16:05
  • shmee, i didn't put it in the body of the question because, as i said in above, i don't know what caused the error, so i tought it could be any thing. That's why i put it on repl.it and github... – Erick César Jan 23 '19 at 16:06
  • The use of formatting in the help string is explained at https://docs.python.org/3/library/argparse.html#help – hpaulj Jan 23 '19 at 17:41

1 Answers1

3

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

Alif Jahan
  • 793
  • 1
  • 7
  • 20
  • 1
    Good find. You could add that this is a problem because `argparse` seems to use `%` formatting in `self._get_help_string(action) % params`, otherwise `%` does not have to be escaped. – tobias_k Jan 23 '19 at 12:49
  • Not downvoting, but this post is likely to cause a hitchhiker situation. If at any point both links from the OP's post go down, this answer is widely useless because no one will really know what the question was. This is only ever okay if the answer is 42 ;) Please try to make sure that the quality of a question is sufficient before answering. – shmee Jan 23 '19 at 13:03