Run Typer scripts with completion, without having to create a package, using Typer CLI.
There is an optional utility tool called Typer CLI, in addition to Typer itself.
It's main feature is to provide β¨ completion β¨ in the Terminal for your own small programs built with Typer.
... without you having to create a complete installable Python package.
It's probably most useful if you have a small custom Python script using Typer (maybe as part of some project), for some small tasks, and it's not complex/important enough to create a whole installable Python package for it (something to be installed with pip).
In that case, you can install Typer CLI, and run your program with the typer-cloup command in your Terminal, and it will provide completion for your script.
You can also use Typer CLI to generate Markdown documentation for your own Typer programs π.
There's nothing wrong with using Python directly to run it. And, in fact, if some other code or program uses your script, that would probably be the best way to do it.
βοΈ But in your terminal, you won't get completion when hitting TAB for any of the subcommands or options, like hello, bye, and --name.
You can also run the same script with the typer-cloup command you get after installing typer-cloup-cli:
fast βtyper-cloup my_custom_script.py run hello Hello World!
typer-cloup my_custom_script.py run hello --name Camila Hello Camila!
type
Instead of using python directly you use the typer-cloup command.
After the name of the file, add the subcommand run.
βοΈ If you installed completion for Typer CLI (for the typer-cloup command) as described above, when you hit TAB you will have β¨ completion for everything β¨, including all the subcommands and options of your script, like hello, bye, and --name π.
importtyper_cloupastyperapp=typer.Typer(help="Awesome CLI user manager.")@app.command()defcreate(username:str):""" Create a new user with USERNAME. """typer.echo(f"Creating user: {username}")@app.command()defdelete(username:str,force:bool=typer.Option(...,prompt="Are you sure you want to delete the user?",help="Force deletion without confirmation.",),):""" Delete a user with USERNAME. If --force is not used, will ask for confirmation. """ifforce:typer.echo(f"Deleting user: {username}")else:typer.echo("Operation cancelled")@app.command()defdelete_all(force:bool=typer.Option(...,prompt="Are you sure you want to delete ALL users?",help="Force deletion without confirmation.",)):""" Delete ALL users in the database. If --force is not used, will ask for confirmation. """ifforce:typer.echo("Deleting all users")else:typer.echo("Operation cancelled")@app.command()definit():""" Initialize the users database. """typer.echo("Initializing user database")if__name__=="__main__":app()