Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic addition of commands to a Group #135

Open
1 task done
eonu opened this issue Jan 3, 2024 · 1 comment
Open
1 task done

Dynamic addition of commands to a Group #135

eonu opened this issue Jan 3, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@eonu
Copy link
Owner

eonu commented Jan 3, 2024

Does this suggestion already exist?

  • This is a new feature!

Feature description

Currently commands must be specified as members of a Group subclass when the subclass is defined, i.e.:

import feud

class CLI(feud.Group):
    def func():
        pass

In some cases, it may be useful to define the group first, then add the command later, e.g.:

import feud

def func1():
    pass

def func2():
    pass

class CLI(feud.Group):
    pass

CLI.add_commands(func1, func2)

Where func1/func2 may be a function, or a click.Command generated from @feud.command/@click.command/@click.group.

Proposed signature is:

import typing

class Group(...):
    def add_commands(
        cls: type[Group], 
        *args: click.Command | typing.Callable,
        **kwargs: click.Command | typing.Callable,
    ):
        ...

If called with keyword arguments, the key should be used as the name of the attribute added to the Group subclass.

The function should update __feud_commands__ on the subclass, and any other relevant attributes. It should probably check that there isn't already a class member with the same name.

If a function is provided, it should be wrapped with @feud.command(config=cls.__feud_config__).

@eonu eonu self-assigned this Jan 3, 2024
@eonu eonu added the enhancement New feature or request label Jan 3, 2024
@eonu
Copy link
Owner Author

eonu commented Jan 7, 2024

Note: If the user is permitted to call Group.add_command on a click.Group, the following should change to include objects in cls.commands() which are click.Groups.

feud/feud/core/group.py

Lines 240 to 245 in 6ad53e6

return [
feud.Section(
name="Command groups",
items=cls.subgroups(name=True),
)
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant