From fab9885c1b5ff6eb0db018c44cef87395397739f Mon Sep 17 00:00:00 2001 From: Sam Watkins Date: Thu, 19 Sep 2024 22:36:04 +1000 Subject: [PATCH] Improve default value handling in help messages - 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 --- src/argh/constants.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/argh/constants.py b/src/argh/constants.py index 07a21d8..91d5f7c 100644 --- a/src/argh/constants.py +++ b/src/argh/constants.py @@ -13,6 +13,7 @@ """ import argparse +import io __all__ = ( "ATTR_NAME", @@ -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 . """ params = dict(vars(action), prog=self._prog) for name in list(params): @@ -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