typer icon indicating copy to clipboard operation
typer copied to clipboard

Hide --install-completion and --show-completion options without disabling autocompletion

Open sidekick-eimantas opened this issue 3 years ago • 4 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the Typer documentation, with the integrated search.
  • [X] I already searched in Google "How to X in Typer" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to Typer but to Click.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

import typer

app = typer.Typer(add_completion=True)
app()

Description

add_completion either turns on or off autocompletion and the --install-completion and --show-completion options. To remove the options, you currently must set add_completion=False, which also disables autocompletion. I would like to decouple the two, and allow removing the options while still retaining the autocompletion capabilities. We manage the autocompletion scripts in our application ourselves, without user input, and don't want the user to have access to these options.

Operating System

Linux, Windows, macOS

Operating System Details

No response

Typer Version

0.7.0

Python Version

Python 3.10.7

Additional Context

No response

sidekick-eimantas avatar Nov 14 '22 10:11 sidekick-eimantas

And how do you manage completion then? How do you install the scripts and how do you call the program to get completion?

tiangolo avatar Nov 14 '22 22:11 tiangolo

And how do you manage completion then? How do you install the scripts and how do you call the program to get completion?

We manage the installation of completion scripts, in the application itself. There's a bunch of things we manage in the application, including rc files, aws profiles, etc. So autocompletion becomes just another thing that we install automatically for the user.

sidekick-eimantas avatar Nov 14 '22 22:11 sidekick-eimantas

You can turn these options into a hidden command

import sys
import typer
import typer.completion
from typer._completion_shared import Shells

app = typer.Typer(add_completion=False)
app_completion = typer.Typer(help="Generate and install completion scripts.", hidden=True)
app.add_typer(app_completion, name="completion")

@app_completion.command(no_args_is_help=True, help="Show completion for the specified shell, to copy or customize it.")
def show(ctx: typer.Context, shell: Shells) -> None:
    typer.completion.show_callback(ctx, None, shell)

@app_completion.command(no_args_is_help=True, help="Install completion for the specified shell.")
def install(ctx: typer.Context, shell: Shells) -> None:
    typer.completion.install_callback(ctx, None, shell)

def main():
    typer.completion.completion_init()
    sys.exit(app())

if __name__ == "__main__":
    main()
$  prog --help
Usage: prog [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.
$ prog completion --help
Usage: prog completion [OPTIONS] COMMAND [ARGS]...

  Generate and install completion scripts.

Options:
  --help  Show this message and exit.

Commands:
  install  Install completion for the specified shell.
  show     Show completion for the specified shell, to copy or customize it.
$ prog completion install --help
Usage: prog completion install [OPTIONS] SHELL:{bash|zsh|fish|powershell|pwsh}

  Install completion for the specified shell.

Arguments:
  SHELL:{bash|zsh|fish|powershell|pwsh}
                                  [required]

Options:
  --help  Show this message and exit.

antoinebrl avatar Mar 05 '23 12:03 antoinebrl

You can turn these options into a hidden command

import sys
import typer
import typer.completion
from typer._completion_shared import Shells

app = typer.Typer(add_completion=False)
app_completion = typer.Typer(help="Generate and install completion scripts.", hidden=True)
app.add_typer(app_completion, name="completion")

@app_completion.command(no_args_is_help=True, help="Show completion for the specified shell, to copy or customize it.")
def show(ctx: typer.Context, shell: Shells) -> None:
    typer.completion.show_callback(ctx, None, shell)

@app_completion.command(no_args_is_help=True, help="Install completion for the specified shell.")
def install(ctx: typer.Context, shell: Shells) -> None:
    typer.completion.install_callback(ctx, None, shell)

def main():
    typer.completion.completion_init()
    sys.exit(app())

if __name__ == "__main__":
    main()
$  prog --help
Usage: prog [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.
$ prog completion --help
Usage: prog completion [OPTIONS] COMMAND [ARGS]...

  Generate and install completion scripts.

Options:
  --help  Show this message and exit.

Commands:
  install  Install completion for the specified shell.
  show     Show completion for the specified shell, to copy or customize it.
$ prog completion install --help
Usage: prog completion install [OPTIONS] SHELL:{bash|zsh|fish|powershell|pwsh}

  Install completion for the specified shell.

Arguments:
  SHELL:{bash|zsh|fish|powershell|pwsh}
                                  [required]

Options:
  --help  Show this message and exit.

Works for me! Thank you @antoinebrl

NikosAlexandris avatar Nov 28 '23 15:11 NikosAlexandris

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

github-actions[bot] avatar May 03 '24 14:05 github-actions[bot]