2

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?

  • You have to specify the new `formatter_class` for the subparsers as well (`add_parser(...)`). They don't inherit that parameter from the main one. – hpaulj Jul 28 '18 at 22:29
  • I understand, but where I should specify formatter_class? In add_subparsers and add_parser there are no such param (https://docs.python.org/3/library/argparse.html#module-argparse) – Darya Litvinchuk Jul 28 '18 at 22:45
  • The `add_parser` method should accept most of the same parameters as the `ArgumentParser` command. Quoting: `This object has a single method, add_parser(), which takes a command name and any ArgumentParser constructor arguments, and returns an ArgumentParser object that can be modified as usual.` – hpaulj Jul 28 '18 at 22:53
  • Thanks :) It's works! PyCharm for some reason did not prompt (and did not see this option, but when I wrote it completely - everything worked!) Thank you again :) – Darya Litvinchuk Jul 28 '18 at 23:16
  • The signature of this method is `add_parser(self, name, **kwargs)`. So there's not a lot for PyCharm to use (compare that with the `__init__` of `ArgumentParser` class). `add_parser` then creates the parser with `self._parser_class(**kwargs)`. – hpaulj Jul 28 '18 at 23:44

0 Answers0