Chapter 65 — Unix-Style CLI Tools
In Chapter 20, you directed Claude Code to build composable Unix tools: sum.py reading from stdin, piping data through filters, writing to stdout. You understood the philosophy. Now you write those tools yourself, with proper argument parsing, exit codes, and tests.
SmartNotes is a Python package. It works perfectly when you import it into Python code. But asking a colleague to open a Python REPL, import your module, and type function calls is not how real software works. Real software has a command-line interface: smartnotes add "Python Tips" "My first note". This chapter builds that interface.
| # | Lesson | Focus | SmartNotes Deliverable |
|---|---|---|---|
| 1 | From Library to Command | sys.argv, the library-to-tool gap | Print sys.argv for smartnotes add "Hello" |
| 2 | Argument Parsing with argparse | ArgumentParser, positional/optional args | smartnotes add <title> <body> via argparse |
| 3 | Subcommands and Help | add_subparsers, help text, dispatch | add/search/list/export subcommands |
| 4 | Exit Codes, stderr, and Environment Variables | sys.exit, sys.stderr, os.environ | Error to stderr, SMARTNOTES_DATA_DIR config |
| 5 | Pipes and Composability | sys.stdin, piped input, stdout for downstream | cat notes.json | smartnotes import |
| 6 | SmartNotes CLI Capstone | Full TDG: [project.scripts] entry point | Complete installable CLI tool |
Prerequisites: SmartNotes package structure (Ch 63), file I/O across three formats (Ch 62), TDG mastery (Ch 57). Students should also recall the stdin/stdout concepts from Chapter 20 (Part 2).