[issue32552] Improve text for file arguments in argparse.ArgumentDefaultsHelpFormatter class

paul j3 report at bugs.python.org
Thu Sep 27 17:11:14 EDT 2018


paul j3 <ajipanca at gmail.com> added the comment:

An alternative to customizing a HelpFormatter is to write your own utility `add_argument` function, e.g.

def my_add_argument(parser, *args, add_default=True, **kwargs):
    if add_default:
        help = kwargs.get('help','')
        help += ' (default: %(default)s)'
        kwargs['help'] = help
    return parser.add_argument(*args, **kwargs)

which could be used as

my_add_argument(parser, '-g', help='bar help', default='other', add_default=False)

There are some refinements to the _get_help_string() that I showed earlier, such as only adding the '%s' to actions where default makes sense (optionals and a subset positionals).  One could also skip it if the default is the default default None, etc.

One way or other the user can already control whether the help line shows the default.  ArgumentDefaultsHelpFormatter just automates this for a straight forward parser.  

I'm going to close this issue since it isn't really needed (and no one has proposed a clever patch).

----------
resolution:  -> rejected
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32552>
_______________________________________


More information about the Python-bugs-list mailing list