Software Engineering / CLI Design
Design CLIs for agents and humans
- MUST provide
--jsonwith a stable, documented schema on every command that outputs data. - MUST write data to stdout and diagnostics, progress and prompts to stderr.
- MUST never block on a prompt when stdin is not a TTY; fail with a clear message and a non-zero exit code instead.
- MUST accept every input as a flag or argument; interactive prompts are a convenience, not the only path.
- SHOULD return distinct, documented exit codes (
0success,1failure,2usage error; others listed in--help). - SHOULD offer
--dry-runon every command that changes state, and--yesto skip confirmation explicitly. - SHOULD make
--helpcomplete and example-led, and keep it in step with behaviour. - SHOULD make output deterministic: stable ordering, no colour or spinners when not a TTY, honour
NO_COLOR. - MAY offer an interactive or TUI mode behind an explicit flag.
When to apply
Section titled “When to apply”Any CLI an agent may call: your own tools, wrappers around APIs, build and ops scripts. Not needed for one-off throwaway scripts that will never be invoked by another program.
Procedure
Section titled “Procedure”- List the commands and, for each, what it reads and what it changes.
- Define the
--jsonshape per command before writing human output. - Define the exit-code table.
- Implement; human output is a rendering of the same data.
- Test with stdin closed (
</dev/null) and stdout piped to confirm nothing blocks or leaks ANSI codes.
Examples
Section titled “Examples”Good:
$ parts list --json[{"id":"R-10K","qty":120}]$ echo $?0Bad:
$ parts list✨ Found 1 part! ✨ R-10K .... 120 leftContinue? [y/N]The bad version mixes decoration with data, has no machine format, and blocks an unattended caller.
Anti-patterns
Section titled “Anti-patterns”- Parsing your own human-readable output in tests or other tools instead of using
--json. - Exit code
0on partial failure. - Changing JSON field names in a minor release.
Checklist
Section titled “Checklist”- Every data command has
--json - Runs unattended with stdin closed
- Exit codes listed in
--help - State-changing commands support
--dry-run