typer logo typer: arguments

1 2 3 4 5 6 7 8 9 10

You typically want to be specific with types in functions from the command line. This is why typer allows you to attach details to your arguments.

The command line app below is the app we end with in the video.

import typer
from typing import List
from pathlib import Path

from .db import app as db_app

app = typer.Typer(name="demo", add_completion=False, help="This is a demo app.")

# Here is where we attach subcommands as a group
app.add_typer(db_app, name="db")

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

def goodbye_world(name):
    """Say goodbye"""
    print(f"goodbye {name}!")

def add(n1: int = typer.Argument(..., help="An integer"),
        n2: int = typer.Argument(1, help="An integer")):
    """Add two numbers"""
    print(n1 + n2)

if __name__ == "__main__":

By adding the argument information we should now see a help text appear for each input but we should now also see proper addition taking place instead of string concatenation.

python cli.py add --help
python cli.py add 1 1