2

The situation

One module (let's call it A) implements a "primary" argparse parser, that parses known arguments common for all children classes (using parser.parse_known_args(argv)). Then, it passes remaining arguments to another object that it's calling. Let's assume it may be either object of class B or C. Both B and C have their own argparse parsers which parse remaining arguments (using parser.parse_args(argv)). They take different arguments, specific to class B or C.

Example snippet from class A:

parser = argparse.ArgumentParser(
    description="Parent parser",
    formatter_class=argparse.RawTextHelpFormatter,
    allow_abbrev=False
)
parser.add_argument('--argument_A', action="append", default=None,
                          help="Help of first argument of parser A")
known, remaining_args = parser.parse_known_args(argv)
my_obj = self.create_obj(b_or_c, remaining_args)

Example snippet from class B:

parser = argparse.ArgumentParser(
        description="Class B parser",
        formatter_class=argparse.RawTextHelpFormatter,
        allow_abbrev=False
    )
    parser.add_argument('--argument_B', action="append", default=None,
                              help="Help of first argument of parser B")
    B_arguments_parsed = parser.parse_args(argv)

Example snippet from class C:

parser = argparse.ArgumentParser(
        description="Class C parser",
        formatter_class=argparse.RawTextHelpFormatter,
        allow_abbrev=False
    )
    parser.add_argument('--argument_C', action="append", default=None,
                              help="Help of first argument of parser C")
    C_arguments_parsed = parser.parse_args(argv)

While implementing passing the arguments and parsing them in the right places was easy, I didn't find a simple solution to print proper help.

The question

How do I implement help message so that my parent parser (from class A) prints it's own help and help from parser B or C?

I would like to see help message from parser A and, depending on which object was selected, parser B or C.

Artur
  • 973
  • 1
  • 14
  • 29
  • Did you have a look at [sub parsers](https://docs.python.org/3.7/library/argparse.html#sub-commands) ? – Graipher Sep 14 '18 at 10:46
  • Thank you for pointing this, I should have mentioned it. For now, there was a design decision to keep the parsers separated because of other project dependencies. This way I can't use sub_parser AFAIK. Maybe, if there is no better way, I will need to refactor to support sub_parsers. – Artur Sep 14 '18 at 10:51
  • The subparser mechanism doesn't display a unified help either. – hpaulj Sep 14 '18 at 16:09
  • You can get a formatted string with `parser.format_help()`, `parserB.format_help()`, etc. But it's up to you to join them. And look at `parser.print_help` and the Help Action subclass for ideas on how to invoke a customized help. – hpaulj Sep 14 '18 at 16:14
  • 1
    Similar issue for subparsers: [Subparser — show help for both parser and subparser](https://stackoverflow.com/questions/32581439/subparser-show-help-for-both-parser-and-subparser) – hpaulj Sep 15 '18 at 16:26
  • Thanks! I think I will manually join helps with `parsers.format_help()`. – Artur Sep 17 '18 at 06:00

0 Answers0