CLI Subcommands
A plugin can add a top-level codumentor <name> ... subcommand. This is not a
slash command. It runs in a fresh CLI process, before any
plugin host exists.
Contribute
Define a classmethod cli_group() on the plugin class. It returns a named
click.Group:
import click
class MyPlugin:
@classmethod
def cli_group(cls) -> click.Group:
@click.group(name="myplugin", help="Manage my plugin's credential.")
def group():
pass
@group.command("status")
@click.pass_context
def status(ctx):
config = ctx.obj["config"] # loaded by the root callback
click.echo(f"instance home: {config.instance_home}")
return group
The group then runs as codumentor [--config X] myplugin status.
When it is available
The command exists only while the plugin is configured and switched on in the
loaded config. The rule is the plugin loader's own: the merged plugins:
entries plus discovered external plugins. A plugin is off when it is in
disabled_plugins: or its entry has args.enabled: false.
- Only the class is imported. Nothing instantiates it, and
register()does not run. Keep the class module cheap to import, and do not rely on anything a plugin instance sets up. - Inside a command,
ctx.obj["config"]is the configuration the root callback loaded, with the CLI overrides applied. - Click resolves a subcommand before the root callback runs. So the CLI loads the config once quietly to find plugin commands: from
--configwhen given, else from the default discovery. It never prompts. If that load fails, plugin commands are simply absent, and core commands still work. codumentor --helplists plugin commands, so it imports the classes of the enabled plugins.
Names
- The group's name is the subcommand. Name it after your plugin.
- A core command always wins a clash. The plugin's command is dropped with a warning on stderr.
- Two enabled plugins that contribute the same name are an error naming both. Switch one of them off.
hidden=Trueon the group keeps it out ofcodumentor --help. It can still be invoked by name.