eonu / feud

Build powerful CLIs with simple idiomatic Python, driven by type hints. Not all arguments are bad.

Home Page:https://feud.wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dynamic addition of commands to a `Group`

eonu opened this issue · comments

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__).

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),
)
]