The Best Way to Make Command-line Interfaces in Python

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

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

Important considerations

Reading arguments using argparse

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

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

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