0

Consider the following code:

import argparse
p = argparse.ArgumentParser(usage="Here we go gathering nuts in May", prog="runfiles.py")
p.add_argument('-a', '--arg0')
s = p.add_subparsers(help="subcommand help", title="subcommands", description="valid subcommands")
sp = s.add_parser(name="subparser1")
sp.add_argument("--arg5", help="help me", metavar="BABYLON5")
sp.add_argument("--arg6", help="help", metavar="BEATLES")
p.parse_args(["--help"])

The output will be:

usage: Here we go gathering nuts in May

optional arguments:
  -h, --help            show this help message and exit
  -a ARG0, --arg0 ARG0

subcommands:
  valid subcommands

  {subparser1}          subcommand help

And if I specify the subcommand, the help message is:

usage: Here we go gathering nuts in May subparser1 [-h] [--arg5 BABYLON5] [--arg6 BEATLES]

optional arguments:
  -h, --help       show this help message and exit
  --arg5 BABYLON5  help me
  --arg6 BEATLES   help

There seems no way to generate help for the --arg5 and --arg6 options, which are specific to the subcommand {subparser1}, at the global level, or for the global argument --arg0 to be described at the subcommand level.

Anyone know how to do this?

mikb
  • 195
  • 13
  • That reflects how the parsers are organized. If `p` sees the `--help` it reports on arguments it handles, including the subparser call. But if it passes control to `subparsers1`, that parser handles the help display. They don't "know" about each others flagged arguments. – hpaulj Nov 03 '20 at 03:20
  • I'm closing this because the **related** column found an old question that's close enough. There may be others as well. – hpaulj Nov 03 '20 at 03:24

0 Answers0