Python: How to remove default options on Typer CLI?

Question:

I made a simple CLI using Typer and Pillow to change image opacity and this program only have one option: opacity.

But when I run python opacity.py --help it gives me the two typerCLI options:

Options:
  --install-completion [bash|zsh|fish|powershell|pwsh]
                                  Install completion for the specified
                                  shell.

  --show-completion [bash|zsh|fish|powershell|pwsh]
                                  Show completion for the specified
                                  shell, to copy it or customize the
                                  installation.

  --help                          Show this message and exit.

There’s a way to disable it? I didn’t find on docs.

Asked By: Zoey

||

Answers:

I met the same problem today, i couldn’t find anything except this question so dived in the source to find how Typer automatically adds this line in app, so i found this, when Typer initialiazing itself it automatically sets add_completion to True

class Typer:
    def __init__(add_completion: bool = True)

So when you initialiazing your app you can add this

app = typer.Typer(add_completion=False)

This is how it looks after you add add_completion=False

Usage: main.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.
Answered By: Yagiz Degirmenci

As of version 0.7.0, Typer now excludes these by default if you use the typer.run() API.

Example:

# foo.py
import typer

def main():
    pass

if __name__ == "__main__":
    typer.run(main)

Produces:

$ pip install typer[all]==0.7.0
$ python foo.py --help

 Usage: foo.py [OPTIONS]

╭─ Options ───────────────────────────────────────╮
│ --help          Show this message and exit.     │
╰─────────────────────────────────────────────────╯
Answered By: carderne