Argparse: Default values not shown for subparsers
<p>I have the problem that I am not seeing any default values for arguments when specifying them via add_argument for subparsers using the argparse Python package.</p>

<p>Some research said that you need non-empty help-parameters set for each add_argument step and you need ArgumentDefaultsHelpFormatter as formatter_class as described here:</p>

<p><a href="http://stackoverflow.com/questions/12151306/argparse-way-to-include-default-values-in-help">Argparse: Way to include default values in &#39;--help&#39;?</a></p>

<p>That's not working for me, however. I suspect that somehow the subparser defaults are suppressed.</p>

<p>Here's an example:</p>

<pre><code>from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

parser = ArgumentParser(description="Sample script", formatter_class=ArgumentDefaultsHelpFormatter, version="sample version")

# Initialize Subparsers

subparsers = parser.add_subparsers(help="", dest="command")

# foo command

fooparser = subparsers.add_parser('foo', help='Do foo')
fooparser.add_argument('files', action='store', help='Foo file(s)' , nargs="+")
fooparser.add_argument("-5", "--Do5", type=int, required=False, dest="do5", help="Do 5 subprocedure.")
fooparser.add_argument("-n", "--topn", type=int, required=False, dest="topn", default=1, help="Show topn")

# bar command

barparser = subparsers.add_parser('bar', help='Do bar')
barparser.add_argument('files', action='store', help='Bar file(s)' , nargs="+")
barparser.add_argument("-mq", "--min-mq", type=int, required=False, default=2, dest="mq", help="Minimum MQ")
barparser.add_argument("-mi", "--min-identity", type=float, required=False, default=0.95, dest="identity", help="Minimum identity")

args = parser.parse_args()
</code></pre>

<p>Specify <code>formatter_class</code> when adding sub-parsers.</p>

<pre><code>subparsers = parser.add_subparsers(help="", dest="command")

fooparser = subparsers.add_parser('foo', help='Do foo',
                                  formatter_class=ArgumentDefaultsHelpFormatter)
...

barparser = subparsers.add_parser('bar', help='Do bar',
                                  formatter_class=ArgumentDefaultsHelpFormatter)
...
</code></pre>

<p>Output of <code>python argparse_test.py --help foo</code>:</p>

<pre><code>usage: argparse_test.py foo [-h] [-5 DO5] [-n TOPN] files [files ...]

positional arguments:
  files                 Foo file(s)

optional arguments:
  -h, --help            show this help message and exit
  -5 DO5, --Do5 DO5     Do 5 subprocedure. (default: None)
  -n TOPN, --topn TOPN  Show topn (default: 1)
</code></pre>

python argparse