click-completion
click-completion copied to clipboard
How to run install command after pip install ?
Question
How to run click_completion.core.install()
command after pip install
complete installing package?
Expected result
$ pip install my-click-app
# Installing click app
# Installing click app completion
# Done
$ myapp g <TAB> <TAB>
$ myapp g
get generate ghost
Current situation
Since I could not find a way to run myapp --completion
within pip install
, I have to run it manually to enable completion. I'm looking for a way to install completion the setup.py of the project.
Current click app code
#!/usr/bin/env python3
"""CLI App module."""
from typing import Any
import click
import click_completion
from click.core import Context
from .delpoy import deploy
from .download import get
from .generator import gen
from .paths import path
def install_callback(ctx: Context, attr: Any, value: Any) -> None:
"""Shell competion function.
Args:
ctx (click.core.Context): Context
attr: Attribute
value: Value
"""
if not value or ctx.resilient_parsing:
return value
shell, path = click_completion.core.install()
click.echo("%s completion installed in %s." % (shell, path))
click.echo("Use ", nl=False)
click.secho(f". {path}", fg="green", bold=True, nl=False)
click.echo(" to activate the completion.")
exit(0)
click_completion.init()
@click.group()
@click.option(
"-c",
"--completion",
is_flag=True,
callback=install_callback,
expose_value=False,
help="Install completion for the current shell.",
)
def cli():
"""Commnadline Interface."""
cli.add_command(deploy)
cli.add_command(gen)
cli.add_command(path)
cli.add_command(get)
Now, with PEP517 and PEP518, I think you should not look for the solution in setup.py, which is a builder IIUC. You should take a look at PEP518 and its implementation to find whatever mecanism they provide.
Please share yours results in here, as I did not take the time to investigate those myself and would love to see a nice solution to your issue :-).
In clk, we provide a bash script that automatize this, but this may not be ideal https://github.com/clk-project/clk/blob/main/install.sh
I believe that finding a PEP518 solution would be more elegant.