The Best Way to Make Command-line Interfaces in Python

How to write easy-to-use, intuitive Python programs

Image for post
Image for post
def main():
first = input(“Enter your first name:”)
last = input(“Enter your last name:”)
print(first + ' ' + last)

Important considerations

When creating a CLI, it is important to consider the following:

  1. Documentation: It is important to write out the function of each option and argument so that a new user can figure out how your program works.
  2. Handle error cases: Let the user know exactly what went wrong and where
  3. Runtime status: If the task does not complete instantly, you should print out the current progress

Reading arguments using argparse

Argparse is a Python standard library module for parsing command-line arguments. You as the programmer can define the arguments that are to be taken and argparse will figure out how to parse those out of sys.argv (a list in Python, which contains the command-line arguments passed to the script, learn more here). Argparse also automatically generates help and usage messages and outputs errors when users give the program invalid arguments. It is very simple to use and makes it very easy to write intuitive CLI’s.

import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
usage: test_cli.py [-h]optional arguments:
-h, --help show this help message and exit
welcome = "Practicing creating interactive command-line interfaces"
parser = argparse.ArgumentParser(description=welcome)
parser.parse_args()

Adding Arguments

Suppose we are writing a program to scrape a webpage. Some arguments we may need are the domain of the webpage --domain or -d , the option to log output to an out file --ofile or -o , and perhaps the option to print a specific number of lines of output to the console --lines or -l. For this example, we will make the domain argument required, while the ofile and lines arguments will be optional.

parser.add_argument('--domain', '-d', required=True, help='domain name of the website you want to scrape. i.e. “https://ahadsheriff.com"')
usage: test_cli.py [-h] --domain DOMAIN
test_cli.py: error: the following arguments are required: --domain/-d
parser.add_argument('--ofile', '-o', help='define output file to save results of stdout. i.e. "output.txt"')parser.add_argument('--lines', '-l', help='number of lines of output to print to the console"', type=int)
args = parser.parse_args()domain = args.domain
ofile = args.ofile
lines = args.lines
print("domain:", domain)
print("output file:", ofile)
print("lines:", lines)

In Conclusion

While this is not a comprehensive guide, it should be enough to get you thinking about command-line interfaces and improving the user experience of your scripts. After all, what’s the point of code if nobody is able to use it. If you have additional recommendations, tips, or resources, please share in the comments!

writing about life, culture, and technology.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store