Commits

Andy Mikhailenko committed 3be2184

Fix #43: append default value of each optional argument to its help message (thanks yAngle)

  • Participants
  • Parent commits b7a62ea

Comments (0)

Files changed (4)

File argh/assembling.py

 from argh.completion import COMPLETION_ENABLED
 from argh.constants import (ATTR_ALIASES, ATTR_ARGS, ATTR_NAME,
                             ATTR_INFER_ARGS_FROM_SIGNATURE,
-                            ATTR_EXPECTS_NAMESPACE_OBJECT)
+                            ATTR_EXPECTS_NAMESPACE_OBJECT,
+                            PARSER_FORMATTER)
 from argh.utils import get_subparsers, get_arg_names
 from argh import compat
 
         # use explicitly defined name; if none, use function name (a_b → a-b)
         cmd_name = getattr(func, ATTR_NAME,
                            func.__name__.replace('_','-'))
-        parser_kwargs = {}
 
-        # add command help from function's docstring
-        parser_kwargs['help'] = func.__doc__
+        parser_kwargs = {
+
+            # add command help from function's docstring
+            'help': func.__doc__,
+
+            # set default formatter
+            'formatter_class': PARSER_FORMATTER,
+
+        }
 
         # try adding aliases for command name
         if SUPPORTS_ALIASES:

File argh/constants.py

 #  General Public License version 3 (LGPLv3) as published by the Free
 #  Software Foundation. See the file README for copying conditions.
 #
+import argparse
+
+__all__ = (
+    'ATTR_NAME', 'ATTR_ALIASES', 'ATTR_ARGS', 'ATTR_WRAPPED_EXCEPTIONS',
+    'ATTR_WRAPPED_EXCEPTIONS_PROCESSOR', 'ATTR_EXPECTS_NAMESPACE_OBJECT',
+    'PARSER_FORMATTER'
+)
+
 
 #
 # Names of function attributes where Argh stores command behaviour
 # forcing argparse.Namespace object instead of signature introspection
 ATTR_EXPECTS_NAMESPACE_OBJECT = 'argh_expects_namespace_object'
 
+#
+# Other library-wide stuff
+#
+
+PARSER_FORMATTER = argparse.ArgumentDefaultsHelpFormatter
+""" Default formatter to be used in implicitly instantiated ArgumentParser.
+"""
+
 #-----------------------------------------------------------------------------
 #
 # deprecated

File argh/dispatching.py

 from argh import compat, io
 from argh.constants import (ATTR_WRAPPED_EXCEPTIONS,
                             ATTR_WRAPPED_EXCEPTIONS_PROCESSOR,
-                            ATTR_EXPECTS_NAMESPACE_OBJECT)
+                            ATTR_EXPECTS_NAMESPACE_OBJECT,
+                            PARSER_FORMATTER)
 from argh.completion import autocomplete
 from argh.assembling import add_commands, set_default_command
 from argh.exceptions import CommandError
 from argh.utils import get_arg_names
 
 
-__all__ = ['dispatch', 'dispatch_command', 'dispatch_commands']
+__all__ = ['dispatch', 'dispatch_command', 'dispatch_commands',
+           'PARSER_FORMATTER']
 
 
 def dispatch(parser, argv=None, add_help_command=True,
 
 def dispatch_command(function, *args, **kwargs):
     """ A wrapper for :func:`dispatch` that creates a one-command parser.
+    Uses :attr:`PARSER_FORMATTER`.
 
     This::
 
 
     This function can be also used as a decorator.
     """
-    parser = argparse.ArgumentParser()
+    parser = argparse.ArgumentParser(formatter_class=PARSER_FORMATTER)
     set_default_command(parser, function)
     dispatch(parser, *args, **kwargs)
 
 def dispatch_commands(functions, *args, **kwargs):
     """ A wrapper for :func:`dispatch` that creates a parser, adds commands to
     the parser and dispatches them.
+    Uses :attr:`PARSER_FORMATTER`.
 
     This::
 
         dispatch(parser)
 
     """
-    parser = argparse.ArgumentParser()
+    parser = argparse.ArgumentParser(formatter_class=PARSER_FORMATTER)
     add_commands(parser, functions)
     dispatch(parser, *args, **kwargs)

File argh/helpers.py

 
 from argh.completion import autocomplete
 from argh.assembling import add_commands, set_default_command
-from argh.dispatching import dispatch
+from argh.dispatching import PARSER_FORMATTER, dispatch
 
 
 __all__ = ['ArghParser']
     wrappers for stand-alone functions :func:`~argh.assembling.add_commands`,
     :func:`~argh.completion.autocomplete` and
     :func:`~argh.dispatching.dispatch`.
+
+    Uses :attr:`~argh.dispatching.PARSER_FORMATTER`.
     """
+    def __init__(self, *args, **kwargs):
+        kwargs.setdefault('formatter_class', PARSER_FORMATTER)
+        super(ArghParser, self).__init__(*args, **kwargs)
+
     def set_default_command(self, *args, **kwargs):
         "Wrapper for :func:`set_default_command`."
         return set_default_command(self, *args, **kwargs)