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

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

Closed
7 tasks done
sidekick-eimantas opened this issue Nov 14, 2022 · 5 comments
Labels
answered question Question or problem

Comments

@sidekick-eimantas
Copy link

First Check

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

Commit to Help

  • 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 sidekick-eimantas added the question Question or problem label Nov 14, 2022
@tiangolo
Copy link
Owner

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

@sidekick-eimantas
Copy link
Author

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.

@antoinebrl
Copy link

antoinebrl commented Mar 5, 2023

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.

@NikosAlexandris
Copy link

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

Copy link

github-actions bot commented May 3, 2024

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 github-actions bot closed this as completed May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered question Question or problem
Projects
None yet
Development

No branches or pull requests

5 participants