Skip to main content

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.

#LessonFocusSmartNotes Deliverable
1From Library to Commandsys.argv, the library-to-tool gapPrint sys.argv for smartnotes add "Hello"
2Argument Parsing with argparseArgumentParser, positional/optional argssmartnotes add <title> <body> via argparse
3Subcommands and Helpadd_subparsers, help text, dispatchadd/search/list/export subcommands
4Exit Codes, stderr, and Environment Variablessys.exit, sys.stderr, os.environError to stderr, SMARTNOTES_DATA_DIR config
5Pipes and Composabilitysys.stdin, piped input, stdout for downstreamcat notes.json | smartnotes import
6SmartNotes CLI CapstoneFull TDG: [project.scripts] entry pointComplete 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).