withfig / autocomplete-tools

Command line tools for working with Fig autocomplete specs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

click_complete_fig does not support a non-group click command

miketheman opened this issue · comments

After seeing support added for Click commands in #53 I tried this on https://github.com/dbcli/pgcli

The command that does not have groups, rather only a root command, fails like so:

$ pgcli generate-fig-spec
Traceback (most recent call last):
  File "/workspace/.pyenv_mirror/user/3.8.13/bin/pgcli", line 5, in <module>
    from pgcli.main import cli
  File "/workspace/.pyenv_mirror/user/3.8.13/lib/python3.8/site-packages/pgcli/main.py", line 1626, in <module>
    fig.add_completion_spec_command(cli)
  File "/workspace/.pyenv_mirror/user/3.8.13/lib/python3.8/site-packages/click_complete_fig/fig.py", line 7, in add_completion_spec_command
    cli.add_command(command)
AttributeError: 'Command' object has no attribute 'add_command'

The project is using Click 8.1.3, but the behavior is the same with Click 7.x as well.

The plugin appears to expect a nested structure which is common, and leverages the add_command to add itself to the command structure, but does not support a root-only command yet.

The simplest repro case is:

import click
from click_complete_fig import fig

@click.command()
def cli():
    click.echo('Hello World!')

fig.add_completion_spec_command(cli)

if __name__ == '__main__':
    hello()

If nothing else, catching the AttributeError and informing the user that a non-grouped CLI is not yet supported would be better than the crash.
Docs currently display the right behavior, but adding a callout might help with any failure or confusion.

I'm fixing this!

@miketheman I am not properly into click but I think group is the only way to add subcommands, right?

Also, I still prefer to get a crashing program. Resilient code is not always the best choice, I will just add a more explicit warning with links and explanation

... group is the only way to add subcommands, right?

I think that it correct, yes.

It may prove beneficial to update the docs as well. https://github.com/withfig/autocomplete-tools/blob/181d2fe067da69b0685619707ec7ee548a530720/integrations/click/README.md

It may prove beneficial to update the docs as well. 181d2fe/integrations/click/README.md

It makes sense.