Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell_completions install? #2127

Closed
hemna opened this issue Nov 9, 2021 · 1 comment
Closed

shell_completions install? #2127

hemna opened this issue Nov 9, 2021 · 1 comment

Comments

@hemna
Copy link

hemna commented Nov 9, 2021

The click-completion package has a mechanism for creating a command for adding the completion code correctly to the running shell's rc file. I don't see a mechanism to do that for shell_completion. Is there a mechanism in click 8 that can provide this? Asking users to copy/paste code for the correct shell is a pain.

For example, my app called aprsd has a completion group that has 2 commands, show, install

aprsd completion show

└─> aprsd completion show
#compdef aprsd
_aprsd() {
  eval $(env COMMANDLINE="${words[1,$CURRENT]}" _APRSD_COMPLETE=complete-zsh  aprsd)
}
if [[ "$(basename -- ${(%):-%x})" != "_aprsd" ]]; then
  compdef _aprsd aprsd
fi

and install

└─> aprsd completion install
zsh completion installed in /Users/me/.zshrc

The code looks like this:

@cli.group(help="Click Completion subcommands", context_settings=CONTEXT_SETTINGS)
@click.pass_context
def completion(ctx):
    pass


# show dumps out the completion code for a particular shell
@completion.command(help="Show completion code for shell", name="show")
@click.option("-i", "--case-insensitive/--no-case-insensitive", help="Case insensitive completion")
@click.argument("shell", required=False, type=click_completion.DocumentedChoice(click_completion.core.shells))
def show(shell, case_insensitive):
    """Show the click-completion-command completion code"""
    extra_env = {"_CLICK_COMPLETION_COMMAND_CASE_INSENSITIVE_COMPLETE": "ON"} if case_insensitive else {}
    click.echo(click_completion.core.get_code(shell, extra_env=extra_env))


# install will install the completion code for a particular shell
@completion.command(help="Install completion code for a shell", name="install")
@click.option("--append/--overwrite", help="Append the completion code to the file", default=None)
@click.option("-i", "--case-insensitive/--no-case-insensitive", help="Case insensitive completion")
@click.argument("shell", required=False, type=click_completion.DocumentedChoice(click_completion.core.shells))
@click.argument("path", required=False)
def install(append, case_insensitive, shell, path):
    """Install the click-completion-command completion"""
    extra_env = {"_CLICK_COMPLETION_COMMAND_CASE_INSENSITIVE_COMPLETE": "ON"} if case_insensitive else {}
    shell, path = click_completion.core.install(shell=shell, path=path, append=append, extra_env=extra_env)
    click.echo(f"{shell} completion installed in {path}")
@davidism
Copy link
Member

davidism commented Nov 9, 2021

I don't plan to add an install mechanism to Click itself. It is too fiddly with many different locations to install the file and many nuances to config files that I don't want to research or maintain.

@davidism davidism closed this as completed Nov 9, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants