Python: Better CLIs with Typer

Build a Python command line tool

Pravash
5 min readApr 28, 2023

--

python_typer

Hi Everyone, In this article, I will explore Typer, a powerful and intuitive library for building CLI applications in Python. And also will explain this topic with example which will help you to get started with it and use it in your day to day coding.

What is Typer

This python library makes it easy to create command-line interfaces for the Python applications. With this we can easily create simplified, user-friendly and robust CLI applications.

Rather than specifying valid CLI arguments using argparse, Typer infers the arguments from the underlying functions. It is based on python type hints.

Install Typer module:

pip install typer

After this, you only need to do is write your own functions and typer will can call it from the command line. Lets see an example -

import typer

app = typer.Typer()

@app.command()
def hello_world():
print("hello world")

@app.command()
def hello(name):
print(f"hello {name}")

if __name__ == "__main__":
app()

In the above code snippet, I am defining 2 functions to print the a message. Now you can call your script from the command-line to perform various tasks:

--

--

Pravash
Pravash

Written by Pravash

I am a passionate Data Engineer and Technology Enthusiast. Here I am using this platform to share my knowledge and experience on tech stacks.

Responses (5)