The Hitchhiker's Guide to CLIs in Python

This article is based on my PyCon 2020 talk of the same title.


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!

Teletypes 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.

Meanwhile, 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.

Teletypes 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.