Creating CLI tool in Python - part 1

Photo by: Lenny Kuhne / Unsplash

This is the first post of automation series, I will create multiple posts about how to automate tasks using the CLI tool. All the resources and software used are linked under the blog post "Resources available" section. In this post, I'm going to set-up a project for my CLI tool and create a "Hello World!" command.

What is a CLI?

Command-line interface (CLI) provides a possibility to interact with the computer using successive lines of text that are invoked through the command-line. As a developer I use CLI tools daily, they are simple to use and often fits my need.
When I did a bit of research about how to build a great CLI experience I discovered a Jeff Dickery presentation Building Great CLI Experiences in Node. I strongly suggest to check it out.

Tooling

For this project I'm going to use Python as a programming language. When lookin for a frameworks for creating a CLI tools in python I discovered a open source package

{' '}

Click. I think click provides all I nead for this project.
To install our tool I'm going to use Python Package Installer a.k.a. pip and package that makes installing easier - Setuptools. To get these tools installed just follow the documentation on each webpage.

Project structure

Copy
1assistant/
2|-- README.md
3|-- assistant.py
4|-- install-dev.sh
5|-- setup.py

assistant.py

This file is an entry point to the CLI tool.

Copy
1def cli():
2 print ('Hello World!')

setup.py

This is a setup file needed for setuptool to make this project installable. In this file, I defined a package name, version, the python modules that should be installed, dependencies that setuptool needs to install and entry points.

Copy
1from setuptools import setup
2
3setup(
4 name='Assistant-Cli',
5 version='0.0.1',
6 py_modules=['assistant'],
7 install_requires=[
8 'Click'
9 ],
10 entry_points='''
11 [console_scripts]
12 assistant=assistant:cli
13 '''
14)

install-dev.sh

This file will contain a script to install an editable package for development. The script will use a Python Package Installer to install this tool for the current user.

Installing and running

This is all I need to get this project print "Hello World!", now try to run it.
First let's install the project as CLI tool using the following command in the project directory.

Copy
1bash install-dev.sh
2
3or
4
5./install-dev.sh

To check if a package got installed following command can be used:

Copy
1pip3 list

And the output should contain 'Assistant-Cli'.
To run the project simply type assistant and this will print "Hello World!".

Summary

In this post, I created an initial project structure, defined a setup.py file for package installer and made the CLI tool to print "Hello World!".
In the next post, I will define basic business logic for assistant and write a first part of the logic - definening a first command and doing some validation for arguments.
This project code is available in Github.

Available resources