Featured Image
Software Development

Build Command Line Interface using Argparse in Python

Building command line interface using Argparse in Python

Introduction

This article will help you to understand and develop command line interface using Argument Parser in python.

What is Argparse?

Parser for command line options, arguments and subcommands. Learn more about argparse from here.

Why use it?

This python module makes easy to write user-friendly command-line interfaces. The program defines what arguments it requires. Argparse will figure out how to parse those out of sys.argv.

Concept

Before developing CLI(Command Line Interface) tool, Let’s have some walk through of Linux command.

When you run ls command in Linux operating system, it displays the list of the files and directories from the current working directory.

When you run ls directory_name, it displays the list of the files and directories from directory_name directory. So here directory_name is known as the positional argument. That means the program know what to do with the value.

When you run ls -l, it displays the list of the files and directories with more information. So here -l is known as an optional argument.

Examples

To start using the argparse module, we first have to import it.

import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
$ python demo.py --help
usage: demo.py [-h]
optional arguments:
  -h, --help  show this help message and exit

Positional Argument

Use add_argument() method to specify command-line options to program.

e.g., I want to print the word in the uppercase letter. Input: congratulations Output: CONGRATULATION

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("word", help="Print the word in upper case letters")
args = parser.parse_args()
print(args.word.upper()) # This way argument can be manipulated.
$ python demo.py congratulations
CONGRATULATIONS

Optional Argument

e.g., I want to print a number of characters in the word. Input: congratulation Output: 14

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("word", help="Print the word in upper case letters")
parser.add_argument("-c", "--count", action="store_true", dest="count", default=False, help="Count number of character in word")  # If option -c is provided than it will have value true based on action attribute. That value will be accessible via attribute value of dest.
args = parser.parse_args()
if args.count:
    print (len(args.word))
$ python demo.py -c congratulation
14

Alternative

You can also use Python Fire to develop CLI. Python Fire is a library for automatically generating command line interfaces (CLI) from absolutely any Python object.


author
Akshay Bosamiya
Experienced Software engineer a demonstrated history of working in the information technology and services industry. Skilled in Python, Django, Go, Amazon web services(AWS), DevOps. Also, have a good experience with building scalable web products. I am a quick learner and like to explore new technologies.