CLI Design Principles: Streams, Flags & Exit Codes

Essential conventions for building command-line tools that integrate cleanly with pipes, scripts, and CI pipelines.

Systems Core Reference 6 min read Nov 28, 2026

Well-designed command-line tools treat the terminal both as a human interface and as a programmable API. Following standard stream and exit conventions prevents brittle shell wrappers.

Standard Streams Contract

Always separate primary payload data from human-readable status messages:

  • stdout (fd 1): Reserved strictly for primary machine-readable or pipeable output (JSON, CSV, file paths, or transformed text).
  • stderr (fd 2): Used for progress indicators, warnings, error diagnostics, and interactive prompts.
# Clean separation lets users pipe stdout safely while still seeing errors
crafttool export --format json | jq '.items[] | .id'

Deterministic Exit Codes

Scripts rely on exit status codes rather than parsing error strings:

Code Meaning Typical Trigger
0 Success Command completed normally
1 General Error Unhandled runtime or I/O failure
2 Usage Error Invalid CLI flag, missing argument, or bad config syntax
64–78 sysexits.h Specialized network, permission, or data format errors

Configuration Precedence

Resolve runtime settings in a strict, documented order from highest to lowest priority:

  1. Explicit command-line flags (--timeout 30)
  2. Environment variables (CRAFTTOOL_TIMEOUT=30)
  3. Local project configuration file (./crafttool.toml)
  4. Global user configuration (~/.config/crafttool/config.toml)
  5. Built-in safe defaults
#cli #unix #systems #developer-tools