Skip to content

CLI tool using typer

I was given a task to write a cli (command line interface) tool in python. Naively, I started with argparse. Which is an awesome library but not beginner friendly. Luckily my mentor/manager stopped me at the right time and introduced me to Typer.

Typer is a library written by Sebastián Ramírez and it makes it really easy to write CLI tools.

It is simple and easy to learn because it lets you write python functions using Typer's decorators. Meaning you can write CLI tools if you know basic python functions.

Project

I'm going to explain what I've learned by creating a CLI tool that parses a CSV file obtained from National Records of Scotland here . It contains ~4000 records with first_name, last_name, number, rank, and sex.

Link to git repo here

The repo contains extensive documentation on how to run commands and what to expect by each of it. However with this post, I will be breaking down the how's/what's/when's of the commands.

For example:

Let's take the first command, read.

It is a python function that uses a decorator with some inputs.

@app.command()
def read(path: Path = typer.Argument(None)):
    """
    1. Name of the command: read
    2. Inputs
        a. path:
        Description: Required input (therefore Argument).
                     By default set to None.
        Type: Path
        Short version: N/A
    """

Usage:

$ cd src

python main.py read ../babies_names.csv 


Now, the second command, find.

@app.command()
def find(
    path: Path = typer.Argument(None),
    fields: Optional[List[str]] = typer.Option(None, "--f"),
    write_path: Path = typer.Option(None,"--w"),
):
    """
    1. Name of the command: find
    2. Inputs
        a. path:
           Description: Required input (therefore Argument).
                        By default set to None.
           Type: Path
           Short version: N/A

        b. fields:
           Description: An optional list of strings. By default
                        set to None.
           Type: List of strings.
           Short version: --f

        c. write_path:
           Description: Optional input (therefore Argument).
                        By default set to None.
           Type: Path
           Short version: --w

    """

Usage:

$ cd src

python main.py find ../babies_names.csv --f year --f sex --w ../write_new_file.csv

That should be more than enough to get you started. For an in-depth explanation please take a look at the repo.

If you have any questions/comments/suggestions reach out to me via twitter