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
Provenance
- Commit
7147f41- Tree
8085d94- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
2571539- Parent 2
3f17cd8
3 files changed+46 added−5 removed
modified:src/click/core.py+20 added-5 removed
1428 unmodified lines1429 return []1430 1431 1432 class 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.1435 1436- :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`.1443+1444+ .. versionchanged:: 8.01445+ The ``commmands`` argument can be a list of command objects.1446 """1447 1448 #: If set, this is used by the group's :meth:`command` decorator1449 #: as the default :class:`Command` class. This is useful to make all15 unmodified lines1465 group_class = None1466 1467 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 {}1469 1470+ if commands is None:1471+ commands = {}1472+ elif isinstance(commands, (list, tuple)):1473+ commands = {c.name: c for c in commands}1474+1475+ #: The registered subcommands by their exported names.1476+ self.commands = commands1477+1478 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 linesmodified:tests/test_basic.py+24 added
78 unmodified lines79 assert "ROOT EXECUTED" in result.output80 assert "SUBCOMMAND EXECUTED" in result.output81 82 83+def test_group_commands_dict(runner):84+ """A Group can be built with a dict of commands."""85+86+ @click.command()87+ def sub():88+ click.echo("sub", nl=False)89+90+ cli = click.Group(commands={"other": sub})91+ result = runner.invoke(cli, ["other"])92+ assert result.output == "sub"93+94+95+def test_group_from_list(runner):96+ """A Group can be built with a list of commands."""97+98+ @click.command()99+ def sub():100+ click.echo("sub", nl=False)101+102+ cli = click.Group(commands=[sub])103+ result = runner.invoke(cli, ["sub"])104+ assert result.output == "sub"105+106+107 def test_basic_option(runner):108 @click.command()109 @click.option("--foo", default="no value")110 def cli(foo):424 unmodified linesmodified:CHANGES.rst+2 added
80 unmodified lines81 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`87 88 89 Version 7.1.290 -------------635 unmodified lines