Command Line Applications + Python-Click + NewsApi

Elizabeth Nakijjo
5 min readNov 19, 2018

--

A command-line interface or command language interpreter (CLI), also known as a command-line user interface, console user interface and character user interface (CUI), is a means of interacting with a computer program where the user (or client) issues commands to the program in the form of successive lines of text.

Command-line applications, also referred to as Console Applications, are computer programs designed to be used from a text interface, such as a shell. Command-line applications usually accept various inputs as arguments often referred to as parameters or sub-commands, as well as options.

Our focus during this project is to be using click though there are other packages like clint, docpot, plac, cliff, cement, python fire also available for use.

Click

click is a Python package for creating command-line interfaces in a composable way with as little code as possible. It aims to make the process of writing command line tools quick and fun while also preventing any frustration caused by the inability to implement an intended CLI API.

Click in three points:

• arbitrary nesting of commands

• automatic help page generation

• supports lazy loading of subcommands at runtime.

Someone might be asking Why Click?

This question is easy to answer: because there is not a single command line utility for Python out there which ticks the following boxes:

• is lazily composable without restrictions

• fully follows the Unix command line conventions

• supports loading values from environment variables out of the box

• supports for prompting of custom values

• is fully nestable and composable

• works the same in Python 2 and 3

• supports file handling out of the box

• comes with useful common helpers (getting terminal dimensions, ANSI colors, fetching direct keyboard input,screen clearing, finding config paths, launching apps and editors, etc.)

Now let’s dive straight into how I created a command line application that consumes the News API using python?

Since am using widows all the commands used here will be supported on the windows platform for others with mac OS and linux you can follow the manuals depending on your operating system.

For your machine to be able to call the pip command make sure the path was installed on or else enter them manually otherwise the command won’t be recognized and you know when that happens , it gave me a lot of headache just to make sure my pip install was recognized by my machine simply because I did not add the path during the installation of python.

The virtual environment is probably what you want to use for developing Click applications. We install virtual environment in our working folder using the command

pip install virtualenv

Once you have your virtualenv installed, just fire up a shell and create your own environment. I usually create a project folder and a venv folder within using this command:

virtualenv venv

In a newly created virtualenv, there will be an activate shell script. For Windows systems run the command below to activate your virtual environment and use ‘deactivate’ to disable

>> .\venv\Scripts\activate.bat

Now, let’s move on. Enter the following command to get Click activated in your virtualenv:

pip install Click

Since we will also be using some get methods in the API it’s also important to install requests by using

pip install requests

Now we are done setting up our developing environment.

Back to the project requirements

Develop a command line application that consumes the News API;
The user is offered a list of four news sources.
The user should be able to make a choice of a preferred news source.
The user gets back a list of the top 10 headlines from the preferred source. A news headline should have a title, description, and an URL in case the user needs to follow up on the news story.

You will also need to have an account created on Newsapi.org in order for you to access data via the API

got to newsapi.org and create an account

First of all let me create a user guide help (menu) for my news app named as NEWPIE, in other words, it contains the options and commands that the user has to work with in order to navigate the application in the console.

Run the following command below to get the result in the console.

newpie

Code snippet:

@click.group()def main(add argument):
"""
Docstring where information about your CLI application will go
"""

@main.command()
def listsources(ctx, sources):
"""
Enter your choice from the source list
"""
pass
@main.command()
def topheadlines():
"""
Get a list of top 10 headlines from the sources
"""
pass

After that, the user runs the command below to get the list of 4 news sources in the console

newpie listsources

Then after the user is prompted to make a choice of a preferred news source by running the command below

newpie topheadlines

After the user enters their choice, the app returns the top-10 headlines with the title, description and url in case the user wants to follow up the news make sure you are referencing the api url in order to collect the data remotely.

Finally for your application to run as an executable by calling the commands directly, for example typing newpie in your console you need to install;

pip install --editable path of your file

FULL SOURCE CODE CAN BE FOUND HERE: GITHUB

Make sure you click the clap button down below and share it with other friends who definitely need this blog

--

--