CLI Development
Build CLIs that stay actionable in both success and failure paths.
Core Principles
- Avoid dead ends.
- Print next steps when a reasonable next action exists.
- On terminal completion, state that no further action is required.
- Make failures diagnosable.
- Print known state, failed operation, and likely cause.
- Include recovery commands and focused help text.
- Include usage text for argument or syntax errors.
- Keep output minimal and context-efficient.
- Use short defaults.
- Show detail only when requested by flags or when needed to recover from failure.
- Scope help to subcommands.
- Keep root help high-level.
- Put detailed flags, examples, and edge cases in subcommand help.
Command Contract
- Keep output deterministic.
- Use stable wording and field names.
- Avoid random ordering in lists.
- Use conventional stream behavior.
- Write primary result data to stdout.
- Write diagnostics, warnings, and human-oriented guidance to stderr.
- In machine mode (
--json), print a complete structured success or error object to stdout. - Document that automation should capture both stdout and stderr for full logs.
- Return meaningful exit codes.
0for success.- Non-zero codes map to clear failure classes.
- Support automation.
- Add machine-readable output mode such as
--json. - Keep human-readable output as the default.
- Add machine-readable output mode such as
- Support safe execution.
- Add
--dry-runfor mutating commands. - Make retry behavior explicit.
- Add
Help and Error Pattern
Use this pattern for each subcommand:
- One-line purpose.
- Usage line.
- Required arguments.
- Optional flags.
- Examples, including one failure-recovery example.
When returning an error, format output in this order:
- Error summary.
- Known state.
- Recovery options.
- Exact next command.
- Relevant subcommand help hint.
Output Templates
Success with next step:
Created release r123.
Next: mycli release publish r123
Success without next step:
Published release r123.
No further action required.
Failure with recovery:
Error: failed to publish release r123 (artifact missing).
Known state: release exists, build step did not produce dist/app.tar.gz.
Try: mycli release build r123
Then: mycli release publish r123
Help: mycli help release publish
Implementation Checklist
- Define root command and subcommand boundaries.
- Write subcommand help before command logic.
- Implement parser and validate required arguments.
- Implement success and failure output contracts.
- Verify stream contract: parseable payloads on stdout, diagnostics on stderr.
- Add tests for success, parser errors, and runtime failures.
- Verify each failure path includes state and next steps.