Calmcode - 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

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

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 add --help
python add 1 1