python LogoCommand Line Interface (CLI) with Typer

A Command Line Interface (CLI) is a text-based interface used to operate software and operating systems by typing commands. CLIs provide a powerful, efficient, and scriptable way for users to interact with programs, often favored by developers and system administrators for automation and precise control.

Typer is a modern, fast, and easy-to-use library for building beautiful command-line interfaces in Python. It's built on top of Pydantic and Click, leveraging Python type hints to automatically define commands, arguments, and options, making CLI development intuitive and robust.

Key features of Typer include:
- Intuitive Argument & Option Declaration: By simply using standard Python type hints in function parameters, Typer automatically infers whether a parameter should be a command argument or an option.
- Automatic Help Messages: Typer generates comprehensive and well-formatted help messages based on your function signatures and docstrings.
- Data Validation: Leveraging Pydantic, Typer can validate input data based on the provided type hints, ensuring your commands receive valid data.
- Rich Integration: It works seamlessly with `rich` for beautiful output, progress bars, and more.
- Auto-completion: Typer can generate shell auto-completion scripts for your CLI, improving user experience.
- Subcommands: Easily organize complex applications into hierarchical commands.

Typer simplifies CLI development significantly. Instead of manually parsing `sys.argv` or using more verbose argument parsing libraries, Typer allows developers to write standard Python functions with type hints, and it handles all the underlying CLI mechanics. This approach leads to more readable, maintainable, and less error-prone CLI applications.

Example Code

python
import typer
from typing_extensions import Annotated

 Create a Typer application instance
app = typer.Typer()

@app.command()
def hello(
    name: Annotated[str, typer.Option(help="The name to greet.")] = "World",
    formal: Annotated[bool, typer.Option(help="Say a formal greeting.")] = False,
):
    """
    Greets a person with a friendly message.
    """
    if formal:
        message = f"Greetings, {name}!"
    else:
        message = f"Hello, {name}!"
    print(message)

@app.command()
def goodbye(
    name: Annotated[str, typer.Argument(help="The name to bid farewell to.")],
    repeat: Annotated[int, typer.Option(help="Number of times to say goodbye.")] = 1,
):
    """
    Bids farewell to a person.
    """
    for _ in range(repeat):
        print(f"Goodbye, {name}!")

if __name__ == "__main__":
    app()


How to run this example:
1.  Save the code: Save the code above as `cli_app.py`.
2.  Install Typer: If you haven't already, install Typer and its optional dependencies: `pip install "typer[all]"`.
3.  Run commands from your terminal:
    -   `python cli_app.py hello` (Output: `Hello, World!`)
    -   `python cli_app.py hello --name Alice` (Output: `Hello, Alice!`)
    -   `python cli_app.py hello --name Bob --formal` (Output: `Greetings, Bob!`)
    -   `python cli_app.py goodbye John` (Output: `Goodbye, John!`)
    -   `python cli_app.py goodbye Jane --repeat 3` (Output: `Goodbye, Jane! \n Goodbye, Jane! \n Goodbye, Jane!`)
    -   `python cli_app.py --help` (Shows general help)
    -   `python cli_app.py hello --help` (Shows help for the `hello` command)