Reusing an existing Click tool with register_commands

The register_commands plugin hook lets you add extra sub-commands to the datasette CLI tool.

I have a lot of existing tools that I'd like to also make available as plugins. I figured out this pattern for my git-history tool today:

from datasette import hookimpl
from git_history.cli import cli as git_history_cli

def register_commands(cli):
    cli.add_command(git_history_cli, name="git-history")

Now I can run the following:

% datasette git-history --help
Usage: datasette git-history [OPTIONS] COMMAND [ARGS]...

  Tools for analyzing Git history using SQLite

  --version  Show the version and exit.
  --help     Show this message and exit.

  file  Analyze the history of a specific file and write it to SQLite

I initially tried doing this:

def register_commands(cli):

But got the following error:

TypeError: Attempted to convert a callback into a command twice.

Using cli.add_command() turns out to be the right way to do this.

Research issue for this: datasette#1538.


Created 2021-11-29T09:32:07-08:00 · Edit