
Build Beautiful CLI Tools in Python with Typer and Rich
Command-line tools are a developer's bread and butter. Python's typer and rich libraries make building them surprisingly pleasant. Setup pip install typer rich Your First CLI with Typer # cli.py import typer app = typer . Typer () @app.command () def hello ( name : str , count : int = 1 ): """ Greet someone multiple times. """ for _ in range ( count ): print ( f " Hello, { name } ! " ) if __name__ == " __main__ " : app () python cli.py Alice --count 3 # Hello, Alice! # Hello, Alice! # Hello, Alice! python cli.py --help # Usage: cli.py [OPTIONS] NAME # Greet someone multiple times. Typer automatically generates help text, validates types, and handles errors. Multiple Commands import typer from typing import Optional app = typer . Typer ( help = " Project management CLI " ) @app.command () def init ( name : str , template : str = " default " ): """ Initialize a new project. """ print ( f " Creating project ' { name } ' with template ' { template } '" ) @app.command () def build ( target
Continue reading on Dev.to Tutorial
Opens in a new tab



