The Hitchhiker’s Guide to Command Line Interfaces in Python

Originally posted on hackernoon.

This article is based on my PyCon 2020 talk of the same title. Originally published at on May 4, 2020.In the beginning, was the command line. Well, not quite the command line we know today.We had typewriters. We were communicating using Morse code. One fine day, someone had an idea to connect a typewriter to an existing network of communication wires. And the teletypewriter was born!

Image: Teletype teleprinters in use during World War II, Source:WikipediaTeletypes removed the need for a human to know Morse code, which improved message typing speed and delivery time. In this 1932 video, the narrator describes how a teletype takes only a matter of seconds to deliver a message from London to Edinburgh. A stark contrast to the earlier ETA of one week — the time taken by a mail coach to undertake the same 600 km journey.

Image: Assembly of IBM 1401 computers, Source:Computer History MuseumMeanwhile, computers were becoming powerful enough to interact with users in real-time. Another fine day, someone had an idea to repurpose a teletype, connect it to a modem, and use it as a remote interface to early computers. This was the origin of the command-line interface!Among these teletypes were the Friden Flexowriter and the Teletype Model 33. Users typed commands after a prompt character was printed on paper. After they were satisfied with the input, they would press Enter which would then send the command to the computer. And finally the output from the computer would be printed on paper again.

Image: The VT100, Source: WikipediaTeletypes were continued to be used as terminals to computers, until video displays came into the picture during the late 1970s. Video terminals quickly became popular input-output devices for computers after the manufacturers moved to a set of common standards.

Today, physical teletypes and video terminals are obsolete. We instead have terminal emulators, which are software simulations of the real thing. But, have modern terminal emulators borrowed any features from the OG metal beasts?

The Guide

Part 1 — Anatomy of a Terminal and CLI
The jargon file. Learn how terminals and CLIs work.Part 2 — Python packages for writing CLIs
Look at packages that can be used to write CLIs, which are available in the Python standard library and on PyPI.Part 3 — Writing and packaging a CLI using Click
Explore various CLI use cases and see how Click can be used to implement a clone of git, with some commonly used git subcommands.Part 4 — User Experience
Look at some principles that can create a nice CLI user experience.

Closing Thoughts

I hope that you got a lot of touchpoints to the CLI ecosystem which you can now explore further, an understanding of how terminals and CLIs work and how to write CLIs in Python!Stay safe and be well.

Source: hackernoon