Skip to content
Myr

Merge pull request #1405 from energizah/commands-list

Before adoption: this commit came in with the history the project adopted, so no landing records it.

support passing a list of commands to Group

authored by David Lord · committed by GitHub · parents 2571539, 3f17cd8 · Browse files · 3 files+46 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+46 added−5 removed

  • tests:+24 added−0 removed
  • other:+22 added−5 removed

3 files+46 added−5 removed

modified:src/click/core.py+20 added-5 removed
1428 unmodified lines
1429 return []143014311432class Group(MultiCommand):1433 """A group allows a command to have subcommands attached. This is the1434 most common way to implement nesting in Click.1433 """A group allows a command to have subcommands attached. This is1434 the most common way to implement nesting in Click.14351436 :param commands: a dictionary of commands.1436 :param name: The name of the group command.1437 :param commands: A dict mapping names to :class:`Command` objects.1438 Can also be a list of :class:`Command`, which will use1439 :attr:`Command.name` to create the dict.1440 :param attrs: Other command arguments described in1441 :class:`MultiCommand`, :class:`Command`, and1442 :class:`BaseCommand`.14431444 .. versionchanged:: 8.01445 The ``commmands`` argument can be a list of command objects.1446 """14471448 #: If set, this is used by the group's :meth:`command` decorator1449 #: as the default :class:`Command` class. This is useful to make all
15 unmodified lines
1465 group_class = None14661467 def __init__(self, name=None, commands=None, **attrs):1468 super().__init__(name, **attrs)1460 #: the registered subcommands by their exported names.1461 self.commands = commands or {}14691470 if commands is None:1471 commands = {}1472 elif isinstance(commands, (list, tuple)):1473 commands = {c.name: c for c in commands}14741475 #: The registered subcommands by their exported names.1476 self.commands = commands14771478 def add_command(self, cmd, name=None):1479 """Registers another :class:`Command` with this group. If the name1480 is not provided, the name of the command is used.1481 """
748 unmodified lines
modified:tests/test_basic.py+24 added
78 unmodified lines
79 assert "ROOT EXECUTED" in result.output80 assert "SUBCOMMAND EXECUTED" in result.output818283def test_group_commands_dict(runner):84 """A Group can be built with a dict of commands."""8586 @click.command()87 def sub():88 click.echo("sub", nl=False)8990 cli = click.Group(commands={"other": sub})91 result = runner.invoke(cli, ["other"])92 assert result.output == "sub"939495def test_group_from_list(runner):96 """A Group can be built with a list of commands."""9798 @click.command()99 def sub():100 click.echo("sub", nl=False)101102 cli = click.Group(commands=[sub])103 result = runner.invoke(cli, ["sub"])104 assert result.output == "sub"105106107def test_basic_option(runner):108 @click.command()109 @click.option("--foo", default="no value")110 def cli(foo):
424 unmodified lines
modified:CHANGES.rst+2 added
80 unmodified lines
81 terminals support these codes. :pr:`1429`82- When using ``CliRunner.invoke()``, the replaced ``stdin`` file has83 ``name`` and ``mode`` attributes. This lets ``File`` options with84 the ``-`` value match non-testing behavior. :issue:`1064`85- When creating a ``Group``, allow passing a list of commands instead86 of a dict. :issue:`1339`878889Version 7.1.290-------------
635 unmodified lines