How I created a command line application with python

nabulo vivian
3 min readSep 20, 2018

--

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, often referred to as flags or switches. Or

In simple terms, a command line program is a program that operates from the command line or from a shell.

Some popular command-line applications include:

  • Grep — A plain-text data search utility
  • curl — A tool for data transfer with URL syntax
  • httpie — A command line HTTP client, a user-friendly cURL replacement
  • git — A distributed version control system
  • mercurial — A distributed version control system primarily written in Python

There are various packages for implementing command line applications some of which are click, clint, pyinquirer, docopt. Among the aforementioned packages, I implemented the command line application using pyinquirer.

Here am going to take you the steps of developing a command line application that consumes the News API.

Before you begin You need:-

  1. An editor that you will use to write the command line application. For my case, I used VScode.
  2. A programming language, python. I chose python because of its flexibility and works well with existing programs. I installed the latest version of python
  3. Virtual environment: Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface. Virtual environment essentially allows you to create a “virtual” isolated Python installation and install packages into that virtual installation. The virtual environment is achieved through running a pip install virtualenv from your projects directory using the command prompt terminal. Virtual environment after being installed, create a venv module by running, “virtualenv venv”

Since am using pyinquirer, Install it in the virtual environment, venv using pip install pyinquirer from the scripts folder of the virtual environment.

After the installations,

  • create a python file ‘cli_newsapi.py’ from where you are going to implement your command line application.
  • In the file, import print_function, unicode_literals, prompt, print_json,style_from_dict,Token.
  • Add some style to your application
  • create a class in your file that will accept news source as an argument
  • Add the following code to your file
  • Create a new object for the class
  • Lastly, activate the virtual environment and run your python file using python filename.py
  • Should expect your output to be similar to this

--

--