Hello rtanpo440.
Python's standard command line parser argparse is nice! I'm new to Python and I'm new to it, but I thought the command line parser was so easy to write.
And argparse will automatically generate help for you. The -h``--help
option is automatically added and you can see it nicely without having to ʻadd_argument` yourself.
Documentation: https://docs.python.org/ja/3/library/argparse.html
However, there is only one case. That is, the format and description of each option is overwhelmed, making it difficult to read.
before
#!/usr/bin/python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--foo', help='Foo foo foo.')
parser.add_argument('-l', '--bar', nargs='+', help='Bar bar bar.')
parser.add_argument('-o', '--long-long-long-option', action='store_true', help='Just a so long option, but do nothing')
parser.parse_args()
By default, the description of each option seems to be displayed from a fixed position, regardless of how many characters the format has. Even if the format is long, it will not wrap and the description will be displayed on the next line, so the format will extend beyond the description column.
This is hard to read !! What should I do ... When I was looking at the document, there seems to be something called HelpFormatter
. As the name implies, it customizes the help format, and it seems that four subclasses with functions such as displaying default values and preserving line breaks are provided by default.
…… But the official docs explained the four subclasses provided by default, but I didn't know how to inherit and customize HelpFormatter
on my own, so refer to its implementation. I tried it by groping.
The explanation on the right side is displayed at the 24th position when the left edge of the screen is 0, so when I searched for 24, I found something like that.
self._max_help_position
seems to be involved in this issue. Then, just forcibly inherit which class and overwrite self._max_help_position
!! Let's try it.
After a lot of experimentation, the code below worked.
Creates ForceOneLineHelpFormatter
by inheriting HelpFormatter
.
after
#!/usr/bin/python3
import argparse
class ForceOneLineHelpFormatter(argparse.HelpFormatter):
def __init__(self, *args, **namedargs):
super().__init__(*args, **namedargs)
self._max_help_position = 999
parser = argparse.ArgumentParser(formatter_class=ForceOneLineHelpFormatter)
parser.add_argument('-m', '--foo', help='Foo foo foo.')
parser.add_argument('-l', '--bar', nargs='+', help='Bar bar bar.')
parser.add_argument('-o', '--long-long-long-option', action='store_true', help='Just a so long option, but do nothing')
parser.parse_args()
I set self._max_help_position
to 999 for some reason, but it seems that it is automatically adjusted internally so that there is not too much space between the format on the left and the description on the right. In other words, if this value is just sufficiently larger than the expected screen width, there is no problem.
And here is the result of actually executing it with --help
.
Oh! Good feeling! But there may be a better way ...
--If the help for argparse is difficult to read due to the optional format and description, create a subclass of ʻargparse.HelpFormatter. --And in
init, set
_max_help_position` to a large value such as 999.
--Then the description position is automatically adjusted and the description is displayed neatly on the right side of the format.