Skip to content

Letting Group(commands=) take a list of Commands #1339

Closed

Description

@energizah

I like to use the class-based API for commands. Group(commands=) takes a dict mapping name to command. So I end up writing:

#!/usr/bin env python3

import click


def f(*a, **kw):
    print(a, kw)


commands = [click.Command("cmd1", callback=f), click.Command("cmd2", callback=f)]


cli = click.Group(commands={c.name: c for c in commands})

if __name__ == "__main__":
    cli()

It's a bit annoying to need the dict comprehension outside, so I'm wondering if you might take an addition in __init__, like

if isinstance(commands, collections.abc.Sequence):
    commands = {command.name: command for command in commands}

so that I could write

click.Group(commands=commands)

instead of

click.Group(commands={c.name: c for c in commands})

in all my scripts.

Activity

  1. jcrotts commented on Jun 13, 2019

    @jcrotts
    Contributor

    I'm not sure adding this to the group init is worth it. The API for group clearly asks for a dictionary of commands. I think making group accept a list or sequence complicates the API, and doesn't provide value outside of the slightly smaller user code.

    Happy to review a PR on it though, if you think it does provide value to Click as a whole.

  2. energizah commented on Oct 8, 2019

    @energizah
    ContributorAuthor

    Here's the PR.

    I realize this adds a little bit of complexity to the constructor, which isn't optimal. On the other hand, the change makes it easier to use the class-based API, which I think is a good thing and on balance worth the small complexity cost.

  3. added this to the 8.0.0 milestone on Aug 14, 2020
  4. locked as resolved and limited conversation to collaborators on Nov 13, 2020
  5. added
    parsingParsing, parameters, commands, chaining, context
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    parsingParsing, parameters, commands, chaining, context

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions