Command Line Interface Programming in Python?

PythonServer Side ProgrammingProgramming

In this section we are going to develop a command line interface using python. But before we deep dive into program, lets first understand command line.

Command line are in use since the existence of computer programs and are built on commands. A command line program is a program that runs from a shell or from a command line

While command line interface provides user interface that is navigated by typing commands at terminals, shells or consoles instead of using the mouse.

A command-line interface(CLI) starts with an executable. There are parameters which we can pass to the script depending how they are developed, like:

  • Arguments: We need to provide this parameter that is passed to the script. In case we don’t provide it, the CLI will through an error. For example, numpy is the argument in this command: pip install numpy.

  • Options: An optional parameter which comes with a name and a value pair like: pip install django –cache-dir ./my-cache-dir where –cache_dir is an option param and the value ./my-cache-dir should be uses as the cache directory.

  • Flags: Another optional parameter which tells the script to enable or disable a certain behaviour for example the –help parameter.

Python provides multiple python package to write command line interfaces such as ‘click’. Click allows us to build command line interfaces with a very few lines of code.

Below is a command line interface program without using click package. Writing CLI program may be not as elegant as the one we got using ‘click’ package, as ‘click’ allows you to follow the “Don’t Repeat Yourself” (DRY) principles.

Command line interface without using click package

import sys
import random

def do_work():
   """ Function to handle command line usage"""
   args = sys.argv
   args = args[1:] # First element of args is the file name

   if len(args) == 0:
      print('You have not passed any commands in!')
      for a in args:
         if a == '--help':
            print('Basic command line program')
            print(' --help -> show this basic help menu.')
            print(' --monty -> show a Monty Python quote.')
            print(' --veg -> show a random vegetable')
         elif a == '--monty':
            print('He’s not the Messiah—he’s a very naughty boy')
         elif a == '--veg':
            print(random.choice(['Tomato','Reddis','Carrot', 'Potato', 'Turnip']))
            print('Unrecognised argument.')

if __name__ == '__main__':


c:\Python\Python361>python --monty
He’s not the Messiah—he’s a very naughty boy

c:\Python\Python361>python --help
Basic command line program
--help -> show this basic help menu.
--monty -> show a Monty Python quote.
--veg -> show a random vegetable

c:\Python\Python361>python --veg

c:\Python\Python361>python --error
Unrecognised argument.

As you can see in above program, it is not providing much flexibility to change an argument name.

Below is the same program using python click package to implement CLI.

import click
import random

@click.option('--monty', default=False, help='Show a Monty Python quote.')
@click.option('--veg', default=False, help='Show a random vegetable.')
def do_work(monty, veg):
""" Basic Click example will follow your commands"""
if monty:
   print('He’s not the Messiah—he’s a very naughty boy')
   if veg:
      print(random.choice(['Tomato','Reddis','Carrot', 'Potato', 'Turnip']))
if __name__ == '__main__':


c:\Python\Python361>python --help
Usage: [OPTIONS]

Basic Click example will follow your commands

--monty TEXT Show a Monty Python quote.
--veg TEXT Show a random vegetable.
--help Show this message and exit.

The above program shows, it’s much easier to write CLI using ‘click’ package and save lot of programmers efforts.

Published on 08-Apr-2019 12:57:31