Did I find the right examples for you? yes no

All Samples(6)  |  Call(2)  |  Derive(0)  |  Import(4)
Adds given functions as commands to given parser.

:param parser:

    an :class:`argparse.ArgumentParser` instance.

:param functions:

    a list of functions. A subparser is created for each of them.
    If the function is decorated with :func:`~argh.decorators.arg`, the(more...)

        def add_commands(parser, functions, namespace=None, title=None,
                 description=None, help=None):
    """Adds given functions as commands to given parser.

    :param parser:

        an :class:`argparse.ArgumentParser` instance.

    :param functions:

        a list of functions. A subparser is created for each of them.
        If the function is decorated with :func:`~argh.decorators.arg`, the
        arguments are passed to :class:`argparse.ArgumentParser.add_argument`.
        See also :func:`~argh.dispatching.dispatch` for requirements
        concerning function signatures. The command name is inferred from the
        function name. Note that the underscores in the name are replaced with
        hyphens, i.e. function name "foo_bar" becomes command name "foo-bar".

    :param namespace:

        an optional string representing the group of commands. For example, if
        a command named "hello" is added without the namespace, it will be
        available as "prog.py hello"; if the namespace if specified as "greet",
        then the command will be accessible as "prog.py greet hello". The
        namespace itself is not callable, so "prog.py greet" will fail and only
        display a help message.

    Help message for a namespace can be also tuned with these params (provided
    that you specify the `namespace`):

    :param title:

        passed to :meth:`argparse.ArgumentParser.add_subparsers` as `title`.

    :param description:

        passed to :meth:`argparse.ArgumentParser.add_subparsers` as
        `description`.

    :param help:

        passed to :meth:`argparse.ArgumentParser.add_subparsers` as `help`.

    .. note::

        This function modifies the parser object. Generally side effects are
        bad practice but we don't seem to have any choice as ArgumentParser is
        pretty opaque.
        You may prefer :class:`~argh.helpers.ArghParser.add_commands` for a bit
        more predictable API.

    .. admonition:: Design flaw

        This function peeks into the parser object using its internal API.
        Unfortunately the public API does not allow to *get* the subparsers, it
        only lets you *add* them, and do that *once*. So you would have to toss
        the subparsers object around to add something later. That said, I doubt
        that argparse will change a lot in the future as it's already pretty
        stable. If some implementation details would change and break `argh`,
        we'll simply add a workaround a keep it compatibile.

    .. note::

       An attempt to add commands to a parser which already has a default
       function (e.g. added with :func:`~argh.assembling.set_default_command`)
       results in a `RuntimeError`.

    """
    if 'function' in parser._defaults:
        raise RuntimeError('Cannot add commands to a single-command parser')

    subparsers = get_subparsers(parser, create=True)

    if namespace:
        # make a namespace placeholder and register the commands within it
        subsubparser = subparsers.add_parser(namespace, help=title)
        subparsers = subsubparser.add_subparsers(title=title,
                                                 description=description,
                                                 help=help)
    else:
        assert not any([title, description, help]), (
            'Arguments "title", "description" or "extra_help" only make sense '
            'if provided along with a namespace.')

    for func in functions:
        # 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
            'help': func.__doc__,

            # set default formatter
            'formatter_class': PARSER_FORMATTER,

        }

        # try adding aliases for command name
        if SUPPORTS_ALIASES:
            parser_kwargs['aliases'] = getattr(func, ATTR_ALIASES, [])

        # create and set up the parser for this command
        command_parser = subparsers.add_parser(cmd_name, **parser_kwargs)
        set_default_command(command_parser, func)
        


src/a/r/argh-0.24.1/argh/dispatching.py   argh(Download)
                            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_spec
    """
    parser = argparse.ArgumentParser(formatter_class=PARSER_FORMATTER)
    add_commands(parser, functions)
    dispatch(parser, *args, **kwargs)
 

src/a/r/argh-HEAD/argh/dispatching.py   argh(Download)
                            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_spec
    """
    parser = argparse.ArgumentParser(formatter_class=PARSER_FORMATTER)
    add_commands(parser, functions)
    dispatch(parser, *args, **kwargs)
 

src/a/r/argh-0.24.1/argh/helpers.py   argh(Download)
 
from argh.completion import autocomplete
from argh.assembling import add_commands, set_default_command
from argh.dispatching import PARSER_FORMATTER, dispatch
 

src/a/r/argh-HEAD/argh/helpers.py   argh(Download)
 
from argh.completion import autocomplete
from argh.assembling import add_commands, set_default_command
from argh.dispatching import PARSER_FORMATTER, dispatch