Typer: Build Powerful CLIs in One Line of Code using Python | by Khuyen Tran | Towards Data Science

Have you ever wanted to quickly add arguments to a Python script on the command line like below?
Being able to insert arguments to the Python script from a terminal allows you and your users to experiment with different parameters easily without looking at the source code.
argparse allows you to write command-line interfaces, but its syntax is long.
Is there a way that you can create command-line interfaces in one line of code like below?
That is when Typer comes in handy.
Typer is a library for building Command Line Interface (CLI) applications based on Python’s type hints. It is created by Sebastián Ramírez, the author of FastAPI.
Typer is short to write and easy to use. To install Typer, type:
pip install typer
To create a CLI for your Python script using Typer, simply add typer.run(your-Python-function) to your code.
Name the file above as get_started.py . Now when you run python get_started.py --help , you should see something like below:
By looking at the help message, we know what argument we should add to the file.
Now try to insert an argument next to python get_started.py and see what we get.
Cool! The output changes as we change the argument.
To add a help message describing what a function does, simply add a docstring to that function.
Okay, cool. But what if users don’t understand what the argument NAME means? That is when typer.Argument comes in handy.
You can use typer.Argument(default_value, help) to add a default value and help message to the argument.
Since there is no default value for the argument name , we use ... as the default value.
And you should see the help message like below:
Note that the argument name is required because there is no default value.
If you add the default value to the argument name , this argument is no longer required.
Now you can run python argument_example.py without inserting any argument!
What if we want users to pick from one of the predefined choices? That could be done by using enum.Enum.
Nice! Now users can only choose one of the options red , yellow , or orange to be the value of the argument color .
In the code below, --is-user and --no-is-user are the CLI options.
While CLI arguments are required, CLI options are not required and start with -- .
To turn a function’s argument into a CLI option, use typer.Option .
Cool! Since the data type of --is-user is bool, the CLI option --no-is-user is also automatically generated!
When we use different CLI options, we can see different outputs like below.
We can also use CLI option with a default value:
Now we can insert a new value into the CLI option time by using --time=value .
Note that since we specified the parametertime as int in the function greeting , we can only insert an integer into time . We will get an error if we insert a string instead.
You can also ask the user to insert CLI option with prompt=True .
Note that Typer automatically turns the CLI option into a reasonable prompt! If you want to customize the prompt, simple replace prompt=True with prompt=question :
Sometimes, users might insert invalid values. Is there a way that we can check these values before using them for our functions?
Yes, that could be done by adding the keyword argumentcallback to either typer.Argument or typer.Option .
In the code below, we pass the function check_if_user_exists to callback . This function checks if the username exists. If the username doesn’t exist, we raise a typer.BadParameter exception.
Adding arguments and options to a function from a terminal is cool. But what if we have more than one function in a Python script? That is when the decorator app.command() comes in handy.
In the code above, we:
create a new typer.Typer() application named app
add @app.command() to the functions we want to create commands with
use app() to create a CLI application with those functions
Now both greeting and say-bye are available to be used as commands!
Simply execute python file_name.py command to use a specific command.
How cool is that?
Congratulations! You have just learned how to use Typer to create CLI applications for your Python script. With Typer, you and your users can insert arguments to your Python script without much effort.
Since it only takes one line of code to create a CLI application for your functions, why not give it a try?
Feel free to play and fork the source code of this article here:
github.com
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: