4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

argparseのhelpに "%" を含めると死ぬ

Last updated at Posted at 2019-11-05

下のようなPythonスクリプトを作ったとする。

import argparse

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

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

これは普通に実行できるが、

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

ヘルプを表示しようとするとValueErrorが出てしまう。

$ 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

エラーメッセージの最後の方に出ている通り return self._get_help_string(action) % params のように出力文字列を整形しているため、
% が含まれると意図しないパースが行われるみたい。

対処は、下のように % を使わないようにする。

import argparse

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

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

または、 %% でエスケープする。

-    help="拡大率(%)")
+    help="拡大率(%%)")

出た。

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

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

optional arguments:
  -h, --help   show this help message and exit
  --zoom ZOOM  拡大率(%)
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?