Skip to content

Commit

Permalink
Improve default value handling in help messages
Browse files Browse the repository at this point in the history
- hide None and False default values
- show names for stdin and stdout
- show class names for other file handles
- use repr() for other default values, as before
  • Loading branch information
sswam committed Sep 19, 2024
1 parent 8d25286 commit fab9885
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/argh/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""

import argparse
import io

__all__ = (
"ATTR_NAME",
Expand Down Expand Up @@ -78,6 +79,10 @@ def _expand_help(self, action):
Ideally this could be achieved by simply defining
:attr:`DEFAULT_ARGUMENT_TEMPLATE` as ``{default!r}`` but unfortunately
argparse only supports the old printf syntax.
If the default value is None or False, don't show it.
If the default value stdin or stdout, show the name not the repr.
If the default value is another file handle, show <class name>.
"""
params = dict(vars(action), prog=self._prog)
for name in list(params):
Expand All @@ -95,14 +100,20 @@ def _expand_help(self, action):
# an IndexError in _format_action)
#
if "default" in params:
if params["default"] is None:
params["default"] = "-"
if params["default"] in [None, False]:
action.default = argparse.SUPPRESS
elif isinstance(params["default"], io.IOBase):
if hasattr(params["default"], "name"):
params["default"] = params["default"].name
else:
params["default"] = f"<{params['default'].__class__.__name__}>"
else:
params["default"] = repr(params["default"])
#
# /

return self._get_help_string(action) % params
string = self._get_help_string(action) % params
return string


#: Default formatter (:class:`CustomFormatter`) to be used in implicitly
Expand Down

0 comments on commit fab9885

Please sign in to comment.