If you give default='-' instead of sys.stdin , the help display will be
the input file/stream. (default: -)
That is, help displays the default string, but FileType converts the '-' to stdin / out.
As shown in AH , you can configure the _get_help_string method. It doesn't matter which class you inherit, since modifying this method is all that ADHF does:
class ArgumentDefaultsHelpFormatter(HelpFormatter): """... """ def _get_help_string(self, action): help = action.help if '%(default)' not in action.help: if action.default is not SUPPRESS: defaulting_nargs = [OPTIONAL, ZERO_OR_MORE] if action.option_strings or action.nargs in defaulting_nargs: help += ' (default: %(default)s)' return help
Please note that all this modification adds a line to the help parameter - only (default: %(default)s)
This means that you can get a similar effect by adjusting your own help lines, for example.
parser.add_argument('--infile', '-i', metavar='File', help='The input file/stream, (default: stdin).', default='-', type=argparse.FileType('r')) parser.add_argument('--whatever-arg', '-w', default='any', help='Change something, (default: %(default)s)')
In other words, it saves the spelling (default: %(default)s) for your 28 arguments.
If you are not comfortable setting up the HelpFormatter class (although this is what developers recommend - with the appropriate caveats), you can customize your own setting. For example, create a simple helper function that adds an extra line to each help line:
def foohelp(astr): return astr + ' (default: %(default)s)' arg1 = parser.add_argument('-f','--fooarg', help=foohelp('help string'))
Speaking about changing the program programmatically, it is worth noting that add_argument creates an Action object. You can save a link to it, as I am here, and configure the settings.
arg1 = parser.add_argument('-f','--fooarg', help='help string') print arg1.help arg1.help = foohelp(arg1.help)
With 30 arguments, you probably did a lot of copy-n-paste to define them, or write various helper functions to optimize your setup. Adding a default display is another of these tasks. You can do this during setup, or you can do it through a custom Formatter.