home / tils / til

Menu
  • GraphQL API

til: datasette_reuse-click-for-register-commands.md

This data as json

path topic title url body html shot created created_utc updated updated_utc shot_hash slug
datasette_reuse-click-for-register-commands.md datasette Reusing an existing Click tool with register_commands https://github.com/simonw/til/blob/main/datasette/reuse-click-for-register-commands.md The [register_commands](https://docs.datasette.io/en/stable/plugin_hooks.html#register-commands-cli) 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](https://datasette.io/tools/git-history) tool today: ```python from datasette import hookimpl from git_history.cli import cli as git_history_cli @hookimpl 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 Options: --version Show the version and exit. --help Show this message and exit. Commands: file Analyze the history of a specific file and write it to SQLite ``` I initially tried doing this: ```python @hookimpl def register_commands(cli): cli.command(name="git-history")(git_history_file) ``` But got the following error: TypeError: Attempted to convert a callback into a command twice. Using [cli.add_command()](https://click.palletsprojects.com/en/8.0.x/api/?highlight=add_command#click.Group.add_command) turns out to be the right way to do this. Research issue for this: [datasette#1538](https://github.com/simonw/datasette/issues/1538). <p>The <a href="https://docs.datasette.io/en/stable/plugin_hooks.html#register-commands-cli" rel="nofollow">register_commands</a> plugin hook lets you add extra sub-commands to the <code>datasette</code> CLI tool.</p> <p>I have a lot of existing tools that I'd like to also make available as plugins. I figured out this pattern for my <a href="https://datasette.io/tools/git-history" rel="nofollow">git-history</a> tool today:</p> <div class="highlight highlight-source-python"><pre><span class="pl-k">from</span> <span class="pl-s1">datasette</span> <span class="pl-k">import</span> <span class="pl-s1">hookimpl</span> <span class="pl-k">from</span> <span class="pl-s1">git_history</span>.<span class="pl-s1">cli</span> <span class="pl-k">import</span> <span class="pl-s1">cli</span> <span class="pl-k">as</span> <span class="pl-s1">git_history_cli</span> <span class="pl-en">@<span class="pl-s1">hookimpl</span></span> <span class="pl-k">def</span> <span class="pl-en">register_commands</span>(<span class="pl-s1">cli</span>): <span class="pl-s1">cli</span>.<span class="pl-en">add_command</span>(<span class="pl-s1">git_history_cli</span>, <span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">"git-history"</span>)</pre></div> <p>Now I can run the following:</p> <pre><code>% datasette git-history --help Usage: datasette git-history [OPTIONS] COMMAND [ARGS]... Tools for analyzing Git history using SQLite Options: --version Show the version and exit. --help Show this message and exit. Commands: file Analyze the history of a specific file and write it to SQLite </code></pre> <p>I initially tried doing this:</p> <div class="highlight highlight-source-python"><pre><span class="pl-en">@<span class="pl-s1">hookimpl</span></span> <span class="pl-k">def</span> <span class="pl-en">register_commands</span>(<span class="pl-s1">cli</span>): <span class="pl-s1">cli</span>.<span class="pl-en">command</span>(<span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">"git-history"</span>)(<span class="pl-s1">git_history_file</span>)</pre></div> <p>But got the following error:</p> <pre><code>TypeError: Attempted to convert a callback into a command twice. </code></pre> <p>Using <a href="https://click.palletsprojects.com/en/8.0.x/api/?highlight=add_command#click.Group.add_command" rel="nofollow">cli.add_command()</a> turns out to be the right way to do this.</p> <p>Research issue for this: <a href="https://github.com/simonw/datasette/issues/1538">datasette#1538</a>.</p> <Binary: 58,592 bytes> 2021-11-29T09:32:07-08:00 2021-11-29T17:32:07+00:00 2021-11-29T09:32:07-08:00 2021-11-29T17:32:07+00:00 3322ac874006f755fec92e2caa623e21 reuse-click-for-register-commands
Powered by Datasette · How this site works · Code of conduct