[docs] [issue9694] argparse required arguments displayed under "optional arguments"

paul j3 report at bugs.python.org
Wed Mar 23 15:21:11 EDT 2016


paul j3 added the comment:

This ArgumentDefaultHelpFormatter issue should probably be raised in its own issue.  It applies to 'required' optionals, but any patch would be independent of the issues discussed here.

This class defines a method that adds the '%(default)' string to the help in certain situations.  It already skips required positionals.  So adding a test for 'action.required' should be easy.

    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

There are 2 easy user fixes.

- a custom HelpFormatter class that implements this fix.  

- 'default=argparse.SUPPRESS' for arguments where you do not want to see the default.  This SUPPRESS is checked else where in the code, but for a required argument I don't think that matters (but it needs testing).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9694>
_______________________________________


More information about the docs mailing list