Auto-completion Using Python typer module

This will create a typer command that we be able to call in our terminal like python, git, etc.

$ pip install typer-cli

And finally, we can install completion for the current shell, and it won’t be required to install it again in the future. 

$ typer –install-completion

Now, we are ready to use the typer feature autocompletion. We just need to use typer command instead of the python command and also add run command after the filename in the CLI.

Here’s how we can run the script in typer CLI.

typer [filename.py] run [function] 

The function is auto-completed when we press the Tab key.

Note: To make the auto-completion work, there should not be any call to app(). If you do not remove this line, it’ll give RecursionError.



Python Typer Module

Typer is a library for building powerful command-line interface applications in the easiest way. It is easier to read and the simplest way to create a command line application rather than using the standard Python library argparse, which is complicated to use. It is based on Python 3.6+ type hints and is built on top of Click(Typer inherits most of the features and benefits of Click), which is a Python package to build a command-line interface. The module also provides automatic help and automatic completion for all the shells. Furthermore, it is short to write and easy to use.

Similar Reads

Installation

To install the Typer module, you need to open up your terminal or command prompt and type the following command:...

Calling typer Function

Here’s an example of calling typer on a function. We will make a function and call the function in CLI. This is a program that will display the message “Hello World!” in the command-line interface....

Pass Arguments in Python typer Module from CLI

...

Getting Help information in typer Module

Let’s modify our program to pass the argument to the main function. The following example has one argument name. When the function is called, we pass “World!” along the parameter name....

Python Program to use typer options and prompt user with [yes/no]

...

Executing Multiple Commands using Python typer Module

Typer help is used to display the documentation of the typer python script. It displays arguments, options, descriptions, etc....

Auto-completion Using Python typer module

...