Let’s Make Command Line Interface In Python

Siddharth Shringi
6 min readSep 4, 2018

--

I’m sure you have used a command line interface(CLI) before. Working with CLI is always fun because it’s like you are giving an order to your computer. CLI is a simple text-based interface in which you give commands in text-form to do a certain task.

When you enter command, CLI process the command accordingly and then give output on the same screen. If you are familiar with Unix commands then you know how it works. Other examples are like Git, Curl and many more.

I used Python library Click to make command line interface. There are also other libraries in Python but I find Click better among the others. Because it uses decorator to make commands and it allows proper nesting of commands. But this feature gives you one drawback which is you can not customize the help pages too much. Read Documentation for more information about Click.

This command line interface is a simple tool to access Github API. You can get information like user-info, list of repositories, and what languages did user use in each repository. The CLI that I made is already on Github and also uploaded on Python Package Index. Its name is Gitlo. In this post, I’ll explain the process of making CLI in Python and also how to put it on the Python Package Index. I’ll be using Python3 to make this tool.

Accessing Github Data

I needed data to make CLI but I was confused how would I do that. Github has public API, that’s why you don’t require API key to use it. Here is the link to Github’s Rest API. You’ll find detail explanation of how you can use Github data. You can see below how I get the user data.

Get single user information via cURL

Now I know that the data which I wanted is there but how can I use this data to make a CLI. I started searching on the internet that how to get data using API in Python. Then I came to know about a Python library called Requests that helps you to send HTTP request.

Before getting started, let’s set up a virtual environment to make CLI. First I create a folder called gitlo and then change directory to gitlo. After that, I create a virtual environment called myvenv.

~$ mkdir gitlo
~$ cd gitlo
~/gitlo$ python3 -m venv myvenv

After that, I activate the virtual environment and then installed the required libraries. Before installing any library or package just make sure your virtual environment is activated. You can come out of virtual environment simply by using command deactivate.

Activating the virtual environment
~/gitlo$ source myvenv/bin/activate
(myvenv) ~/gitlo$
Installing the libraries
(myvenv) ~/gitlo$ pip install requests
(myvenv) ~/gitlo$ pip install click

So the project structure will be like, there would be a parent folder called gitlo and in this folder, there would be two files. one is gitlo.py and other is setup.py file.

-gitlo
|-- gitlo.py
|-- setup.py

Main code will be in gitlo.py file and setup.py file will help us to make it command line and Python package.

gitlo.py

gitlo.py
  • First I imported click and requests python libraries. Now let’s understand how click works. Clicks uses decorators to make commands. If you use click.command() decorators to any function then it’ll start behaving as a command.
  • But here I used nesting commands concept of click library. I used click.group() decorator to make multiple subcommands. This decorator is applied on function cli, so now cli.command() can be used to make several commands.
  • Docstring in cli function is a help text. when someone write gitlo --help, this text will appear in terminal.
  • On user function, first we use two decorators; one is cli.command() to make it subcommand and other is click.argument() to give arguments in commands.
  • The data we are getting using requests library is in JSON format. So there are many key-value pairs as you can see in above image in which we use curl command to see the user information.
  • But I wanted to show only name, repositories and bio field that’s why I printed only these field.

setup.py

Setup.py file is necessary because it helps first is to make command executable and for putting this onto Python Package Index.

setup.py

Here I defined some parameters in the setup function. Parameter entry_points is responsible to make command executable. The script that should be generated comes before the equal sign and after equal sign is the import path followed by a colon with Click command.

  • I also add README.rst file and LICENSE file in the project. Readme file is the description of the tool. Classifiers tell some additional metadata about the package. In this case, package is only compatible with Python3 and is licensed under MIT license.
  • Now the magic will happen.
(myvenv) ~/gitlo$ pip install --editable .
  • Now gitlo command is available on the command line. Now if I write gitlo user <username>, it will give the name, number of repositories and bio of the user.
(myvenv) ~/gitlo$ gitlo user siddharthshringiName: Siddharth Shringi, Repos: 18, Bio: Python Developer | ML Enthusiast

Putting on Python Package Index

I’ve added two more commands in the tool. One command is to get list of repositories and other is to know the language percentage of each repository. You can see them on Github repository. Now I’ll show you how I put it on python package index.

  • Now to put this CLI to Python Package Index, I’ve to generate distribution packages which are archives that can be installed through pip.
  • First make sure that setuptools and wheel are installed in upgraded version.
(myvenv) ~/gitlo$ python3 -m pip install --user --upgrade setuptools wheel
  • Now latest version of setuptools and wheel is installed so I need to run one more command to generate distribution packages.
(myvenv) ~/gitlo$ python3 setup.py sdist bdist_wheel
  • This command will create a directory called dist and dist will have two files which will be uploaded to Python Package Index.
dist\
--| gitlo-1.0.1-py3-none-any.whl
--| gitlo-1.0.1.tar.gz

Next step is to create an account on pypi website. After that it’s just two steps away from uploading a package on Python Package Index.

  • First twine need to be installed and then twine will do the rest of the work.
(myvenv) ~/gitlo$ python3 -m pip install --user --upgrade twine(myvenv) ~/gitlo$ twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
Enter your username: <username>
Enter your password:

Gitlo is now python package that you can install through pip. For more information about packaging and distributing projects read this documentation.

pip install gitlo

One fun fact I would like to share that when I was uploading this package to pypi, I was unable to do so. Initially, I named it Gitpy but I forgot to check that if it’s already available on website or not. It took me one day to figure out that the package with the same name already exists on site.

So I changed the name to Gitlo and upload it again. It was a silly mistake but I enjoyed the process of making my first python package.

Follow me on twitter for more updates.

--

--