typer logo typer: introduction

1 2 3 4 5 6 7 8 9 10
Notes

You can run python from the terminal and you're even able to pass in arguments using just the base library.

# cli.py
import sys

def hello_world(name):
    print(f"hello {name}!")

if __name__ == "__main__":
    print(sys.argv)
    hello_world(sys.argv[1])

If you were to run cli.py you would be able to use the program and send arguments to it.

python cli.py vincent

However, there are some utilities missing. So let's make a better command line experience by using typer.

Using Typer

Before you're able to use typer, you need to make sure that it is installed.

pip install typer

Here's the same app, but now with typer.

import typer

def hello_world(name):
    """This prints your name"""
    print(f"hello {name}!")

if __name__ == "__main__":
    typer.run(hello_world)

When you run this from the command line, the experience is a lot better.