Skip to content

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

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions