Remember the last time you had to build a command-line tool? If you’re like me, you probably started with argparse
or click
, wrote boilerplate code, and still ended up with something that felt clunky. That’s where typer comes in – it’s a game-changer that lets you build CLI apps with minimal code. Although there are several other options, typer stands out because it leverages Python’s type hints to do the heavy lifting. No more manual argument parsing! The following snippet shows how to use typer in its simplest form:
import typer app = typer.Typer() @app.command() def hello(name: str): typer.echo(f"Hello {name}!") if __name__ == "__main__": app()
And you will be able to execute it with just:
$ python hello.py Pedro Hello Pedro!
In this simple example, we were only defining positional arguments, but having optional arguments is as easy as setting default values in the function signature.
Continue reading