Include "%" in argparse help to die

Suppose you create a Python script like the one below.

import argparse

parser = argparse.ArgumentParser(description='')
parser.add_argument('--zoom',
    action='store',
    type=float,
    default=1.0,
    help="Expansion rate(%)")
args = parser.parse_args()

print("zoom is {}".format(args.zoom))

This can be done normally,

$ python hoge.py --zoom 2.0
zoom is 2.0

I get a Value Error when I try to display help.

$ python hoge.py -h
Traceback (most recent call last):
  File "hoge.py", line 9, in <module>
    args = parser.parse_args()
  File "C:\Python37\lib\argparse.py", line 1749, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "C:\Python37\lib\argparse.py", line 1781, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "C:\Python37\lib\argparse.py", line 1987, in _parse_known_args
    start_index = consume_optional(start_index)
  File "C:\Python37\lib\argparse.py", line 1927, in consume_optional
    take_action(action, args, option_string)
  File "C:\Python37\lib\argparse.py", line 1855, in take_action
    action(self, namespace, argument_values, option_string)
  File "C:\Python37\lib\argparse.py", line 1037, in __call__
    parser.print_help()
  File "C:\Python37\lib\argparse.py", line 2474, in print_help
    self._print_message(self.format_help(), file)
  File "C:\Python37\lib\argparse.py", line 2458, in format_help
    return formatter.format_help()
  File "C:\Python37\lib\argparse.py", line 284, in format_help
    help = self._root_section.format_help()
  File "C:\Python37\lib\argparse.py", line 215, in format_help
    item_help = join([func(*args) for func, args in self.items])
  File "C:\Python37\lib\argparse.py", line 215, in <listcomp>
    item_help = join([func(*args) for func, args in self.items])
  File "C:\Python37\lib\argparse.py", line 215, in format_help
    item_help = join([func(*args) for func, args in self.items])
  File "C:\Python37\lib\argparse.py", line 215, in <listcomp>
    item_help = join([func(*args) for func, args in self.items])
  File "C:\Python37\lib\argparse.py", line 525, in _format_action
    help_text = self._expand_help(action)
  File "C:\Python37\lib\argparse.py", line 614, in _expand_help
    return self._get_help_string(action) % params
ValueError: unsupported format character ')' (0x29) at index 5

As you can see at the end of the error message, the output string is formatted like return self._get_help_string (action)% params. It seems that unintended parsing is done when % is included.

The workaround is to avoid using % as shown below.

import argparse

parser = argparse.ArgumentParser(description='')
parser.add_argument('--zoom',
    action='store',
    type=float,
    default=1.0,
-    help="Expansion rate(%)")
+    help="Expansion rate(percent)")
args = parser.parse_args()

print("zoom is {}".format(args.zoom))

Alternatively, escape with %%.

-    help="Expansion rate(%)")
+    help="Expansion rate(%%)")

It came out.

$ python hoge.py -h
usage: hoge.py [-h] [--zoom ZOOM]

optional arguments:
  -h, --help   show this help message and exit
  --zoom ZOOM magnification(percent)
$ python hoge.py -h
usage: hoge.py [-h] [--zoom ZOOM]

optional arguments:
  -h, --help   show this help message and exit
  --zoom ZOOM magnification(%)

Recommended Posts

Include "%" in argparse help to die
Automatically register function arguments to argparse in Python
How to get help in an interactive shell
Attempt to include machine learning model in python package
Covector to think in function
To flush stdout in Python
Include GitHub repository in requirements.txt
Porting from argparse to hydra
Login to website in Python
How to use Python argparse
Speech to speech in python [text to speech]
How to develop in Python
Post to Slack in Python