Hi everyone :)
I need help with add_parser () help customization in subparser.
(Not in this parser : Python argparse: How to insert newline in the help text?).
I've redefined _fill_text and _split_lines methods and everything works well for the parser:
class MultilineFormatter(argparse.HelpFormatter):
def _fill_text(self, text, width, indent):
return ''.join([indent + line.strip(' ') for line in text.splitlines(True)])
def _split_lines(self, text, width):
return [line.strip() for line in text.splitlines()]
def get_parsed_data():
parser_ = argparse.ArgumentParser(description=("""String description parser 1
String description parser 2
String description parser 3 """),
formatter_class=MultilineFormatter)
subparsers = parser_.add_subparsers(dest="first", title="Title1", description=("""String description parser 4
String description parser 5
String description parser 6 """))
subparsers.required = True
example_subparser = subparsers.add_parser("sub", help="""String help parser 1
String help parser 2
String help parser 3""")
_example_initialize_subparser(example_subparser)
return parser_.parse_args()
def _example_initialize_subparser(parser_):
subparsers_ = parser_.add_subparsers(dest="second",
description=("""String description subparser 1
String description subparser 2
String description subparser 3 """))
subparsers_.required = True
example_show_parser = subparsers_.add_parser("show", help="""String help subparser 1
String help subparser 2
String help subparser 3""")
_example_initialize_show_parser(example_show_parser)
def _example_initialize_show_parser(parser_):
parser_.add_argument("-d", "--details",
help="""String help subparser 4
String help subparser 5
String help subparser 6""")
It's okay: (Beautiful output, all as I wanted)
python3 main.py -h !2737
usage: main.py [-h] {sub} ...
String description parser 1
String description parser 2
String description parser 3
optional arguments:
-h, --help show this help message and exit
Title1:
String description parser 4
String description parser 5
String description parser 6
{sub}
sub String help parser 1
String help parser 2
String help parser 3
(venv)
But, as you can see, the newline (\n) does not work here:
python3 main.py sub -h !2734
usage: main.py sub [-h] {show} ...
optional arguments:
-h, --help show this help message and exit
subcommands:
String description subparser 1 String description subparser 2 String
description subparser 3
{show}
show String help subparser 1 String help subparser 2 String help
subparser 3
(venv)
And here:
python3 main.py sub show -h !2736
usage: main.py sub show [-h] [-d DETAILS]
optional arguments:
-h, --help show this help message and exit
-d DETAILS, --details DETAILS
String help subparser 4 String help subparser 5
String
help subparser 6
(venv)
So, how to make newline work in subparser?