How to create a command line application using a Python library — Typer

Anozie Baron Chibuikem
3 min readAug 30, 2022

Just like the official documentation says,

Typer is a library for building CLI applications that users will love using and developers will love creating. Based on Python 3.6+ type hints.

I found this library awesome for building a CLI application and in this article we will be building a CLI application using it.

WHAT ARE WE BUILDING?

We will be developing a python script that will enable you to create mock data on a MongoDB using the command line.
This can be useful in scenarios where a new developer joins your team but you don’t want the developer having access to the various hosted databases the company uses yet, the developer can instead run the script and it will setup a replica of your database locally (on MongoDB) or any DB you choose.

For this article, we will be using just 3 collections, Company, Department, and Project.
The workflow is such that, every “Project” belongs to a particular “Department” and in return, every Department belongs to a “Company”. With that in mind, this is a representation of they data fields in our respective documents inside our local MongoDB collection(which we will name “localdb”)

company= {_id, name, createdAt, createdBy, updatedAt, updatedBy}department= {_id, name, companyId, teamLead, updatedAt, createdBy, createdAt, updatedBy}project= {_id, name, companyId, createdBy, dueDate, assignedToUserId, createdAt, updatedAt, updatedBy}

LET'S START BUILDING😊

Create a new folder called ‘mockdb’ and inside create a python file called ‘create_mock_data.py’.

Setup a virtual environment, activate it, and install the following libraries

pip install pymongo
pip install "typer[all]"

We will be using Typer decorator command, which you can find in their official documentation

Let's start by creating our first command function,

create_new_mock_data as the name implies is the function we will be calling in our command line to create new mongo data.

The function arguments.
1. number_of_data_to_create: this will take the number of instances we want to create in our company, department, and project documents. So if we pass 1 as the argument, only one data instance will be created for each document.
2. db_stage: this will be used to indicate which database we want to create the mock data in, either on our local MongoDB or a hosted MongoDB.
3.
user_id: this will take in the id of a user we want to attach to some of the fields inside our various document
4.
version: this is used to see the version of the CLI application

The function body.
we have above four functions inside the body of our create_new_mock_data.
1. _create_database: this function is used to set up our chosen database.
2.
_create_company: this function is used to create an instance of a company
3.
_create_department: this function is used to create an instance of a department
4.
_create_project: this function is used to create an instance of a project

The callbacks attached to the function arguments are used for validations.

Next, lets create our _create_database function and our 2 callback validators _db_name_validation_callback and _version_callback

_db_name_validation_callback: this will validate the value entered in create_new_mock_data `db_stage` argument. It’s been configured to allow only staging and localdb as values.
localdb is to represent that you want to create the instance on a locally setup mongodb
staging is to represent that you want to create the instance on a hosted mongodb instance

_version_callback: basically return the current version of the cli application

_create_database: this indicates which database we want to create the data on base the the value of the argument entered. We assume you have 2 seperate ‘env files’ named ‘local.env’ and ‘staging.env’ with a environment value DB_URL declared inside each one.
An example can be
staging.env
DB_URL: ‘your hosted mongodb instance url’
local.env
DB_URL: ‘your local mongodb instance url which likely might be
mongodb://localhost:27017

With the database setup now complete, lets add our respective functions for creating instances on our respective document

For our second command function

This is used for retrieving the values in any document entered. eg companys, departments, projects.

To run our create_new_mock_data and retrieve_collection_data, open your terminal in the path where your project is and use the following command

To run the command to create mock data
python path_to_your_mockdb/mockdb/create_mock_data.py odetask-new-mock-data
To run the command to retrieve data
python path_to_your_mockdb/mockdb/create_mock_data.py retrieve-collection-data
To see the help command
python path_to_your_mockdb/mockdb/create_mock_data.py --help
To see the help command for creating new mock data
python path_to_your_mockdb/mockdb/create_mock_data.py odetask-new-mock-data --help
To see the help command for retrieving values in a document
python path_to_your_mockdb/mockdb/create_mock_data.py retrieve-collection-data --help

Thanks for reading this article, I will see you on another cool topic next time. Till then,

Stay Safe..! Keep Learning..!

Thank you for reading!

Follow me on Medium for the latest updates. 😃.

--

--

Anozie Baron Chibuikem

A backend engineer constantly building a shit ton of things with python and javascript and occasionally writes on technical topics.