I'm trying to get the help screen of the sub-parsers to show the required arguments ABOVE (before) the optional args.
I followed the last answer given at Argparse: Required arguments listed under "optional arguments"?, but could not get the required args to appear above the optional args.
Here is my code snippet:
## using Python 3.6.3
import argparse
from argparse import RawTextHelpFormatter
main_parser = argparse.ArgumentParser(prog="myProg")
subparsers = main_parser.add_subparsers()
## common to all sub-parsers
common_parser = argparse.ArgumentParser(add_help=False)
common_parser.add_argument('foo')
optional = common_parser._action_groups.pop()
required = common_parser.add_argument_group('required arguments')
required.add_argument("-p", type=int, required=True, help='help for -p')
optional.add_argument('-x', help='help for -x')
common_parser._action_groups.append(optional)
abcd_parser = subparsers.add_parser("abcd", parents=[common_parser])
wxyz_parser = subparsers.add_parser("wxyz", parents=[common_parser])
args = main_parser.parse_args()
The output is:
$ ./myProg abcd -h
usage: myProg abcd [-h] -p P [-x X] foo
positional arguments:
foo
optional arguments:
-h, --help show this help message and exit
-x X help for -x
required arguments:
-p P help for -p
However, i would like the output to look like:
$ ./myProg abcd -h
usage: myProg abcd [-h] -p P [-x X] foo
positional arguments:
foo
required arguments:
-p P help for -p
optional arguments:
-h, --help show this help message and exit
-x X help for -x
Is it possible to get the desired results? What needs to be done?
Thanks
--Andrew