Calmcode - typer: commands

Adding Commands in Typer

1 2 3 4 5 6 7 8 9 10

Just like a python library usually has multiple functions a command line app usually has multiple commands. So let's make a typer command line application that can handle multiple actions.

# cli.py
import typer

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}!")

if __name__ == "__main__":
    app()

You should now be able to run the following commands;

python cli.py --help
python cli.py hello-world vincent
python cli.py goodbye-world vincent