The Hitchhiker's Guide to CLIs in Python
Vinayak Mehta (~vinayak-mehta) |
Command-line applications and interfaces are used by both newcomers and experienced Python developers everyday. But do you know how they came to be? Hop on to this ship as we go through the CLI galaxy and look at its history, explore the CLI anatomy and discover some Python packages that can help us create them.
We’ll then look at some widely used CLIs of our time. And emulate one of them by creating our own CLI using Click. Finally, we’ll package it and publish it on PyPI. Are you ready to travel faster-than-light using this ship’s Infinite Improbability Drive? Carry your towel!
- History (2 min)
- Originated as two humans communicating with text lines
- Soon one human replaced by a computer
- Terminal emulators
- Anatomy of a CLI (7 min)
- termios and stty, the illusion!
- Go through some termios settings
- Signals, control characters, escape sequences
- Streams, stdin, stdout, stderr
- Redirection operators
- Command-line interfaces
- Arguments and options
- Help text and man pages
- Standards, POSIX and XDG base directory spec
- Python packages for writing CLIs (7 min)
- Show some code of how a CLI would look with each of the following packages
- Standard library
- Builds a CLI with a documentation first approach
- Restricted to basic parsing
- Does not handle types
- Talk about some limitations for packages mentioned
- Why Click?
- Arbitrary nesting of commands
- Strong understanding of types
- Writing and packaging a CLI using Click (7 min)
- Build a git clone, as almost everyone has a mental model for git
- Show how the following use cases and git commands can be implemented
- Progress bars, git clone
- Application folders, git config
- Paged output, git log
- Colored text, git status
- Launching text editors, git commit
- User prompts, git push
- Talk about testing Click code
- Packaging a Click CLI and publishing it to PyPI
- CLI User Experience (2 min)
- Unix philosophy
- Fish shell design philosophy
- Questions (5 min)
This talk is aimed to be a guide in writing CLIs, for Python developers of all levels. No background knowledge is required.
Video: https://www.youtube.com/watch?v=Hn-syMunNy8 (PyCon US 2020)
Wannabe hoopy frood. Working on open-source developer tools.
I love building CLI tools! Here are some open-source CLIs that I’ve built using Click:
- https://github.com/vinayak-mehta/conrad - Track conferences and meetups on your terminal!
- https://github.com/vinayak-mehta/nbcommands - Unix commands for Jupyter notebooks!
- https://github.com/camelot-dev/camelot - Extract tables from PDFs!
- https://github.com/camelot-dev/excalibur - Extract tables from PDFs! (CLI for the web interface)