Calmcode - typer: arguments

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.")

@app.command()
def hello_world(name):
    """Say hello"""
    print(f"hello {name}!")

@app.command()
def goodbye_world(name):
    """Say goodbye"""
    print(f"goodbye {name}!")

@app.command()
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__":
    app()

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