Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Automation & toolingMixed 22 series

Designing a CLI people keep using

A command-line tool is an interface with no undo, and people forgive it only while it is predictable.

Quick reference

The decisions that separate a tool from a habit.

  1. First run — Help with no arguments, a plan before any write, and exit 2 for a bad flag.
  2. Inputs — Flags over environment over config, one printed source, prompts only on a TTY.
  3. Output — Data on stdout, prose on stderr, and an exit code that means what it says.
  4. Longevity — Short verbs, a versioned format, completions and a man page.

In one line: Small decisions, one habit. The tool lasts because the second year is as cheap as the first week.

Start this week:

  • Run the checklist against the last CLI you shipped.
  • Add --dry-run before you add the next feature.
  • Read your own error messages out loud, then rewrite them.

Explained

The first run. No arguments should print help, never a prompt and never a change. Help lists every flag with its default, and a bad flag exits 2 with the flag, the value and one working example.

The inputs. Flags beat environment variables, environment beats the config file, and the tool prints the resolved value with its source. A prompt is a courtesy at a TTY and a hang in CI, so ask only when stdin is a terminal and always provide --yes.

The output. Data goes to stdout, prose to stderr, and the exit code is the result. Zero means the work happened, not that the process ended. A partial write that exits 0 is the most expensive bug in the category.

The long tail. Short verbs, a versioned output format, shell completion and a man page. Those are read by people who never open the README.

Checklist

Run it before the first release and after every flag change. A line that needs a meeting is a line that will not hold.

First run

  • No arguments prints help and changes nothing.
  • --help lists every flag with its default.
  • A bad flag exits 2 and names the fix.

Inputs

  • Every flag has a fallback, and the tool prints its source.
  • Prompts appear only when stdin is a TTY.
  • --yes exists, and the plan is printed first.

Output

  • stdout carries data only; progress goes to stderr.
  • Exit 0 means the work happened, not that the process ended.
  • Every error names the object, the action and the next step.

Longevity

  • --version prints both the version and the commit.
  • Completion and a man page ship with the binary.
  • The output format is versioned, and a rename is a major change.

The field guide

The full guide of the PDF (P1053), page by page.

The first ten seconds (first contact)

Most people meet a CLI through a failed copy and paste. The first run decides whether they read the help or leave.

  • No arguments prints help, not a prompt and not a change.
  • Every flag is visible in --help, including its default value.
  • The tool says what it will do before it does it.
  • A bad flag prints the flag, the value and one example that works.

Argparse: usage and exit 2 come free

import argparse
 
p = argparse.ArgumentParser(prog="acme", allow_abbrev=False)
p.add_argument("--dry-run", action="store_true")
p.add_argument("--output", choices=["table", "json"], default="table")
args = p.parse_args()
MomentWhat the user needs
--helpUsage, flags and one example
bad flagThe flag, the value, the fix
successOne line and exit 0

The ten second rule If --help takes longer than ten seconds to read, the tool has too many flags for the job. Split it into subcommands.

The first run is a demo, and the tool should give it without being asked.

Flags first, prompts last (inputs)

Flags compose in scripts, prompts do not. A prompt is a courtesy for a human at a terminal and a trap in CI.

  1. Put flags above environment variables, and env above the config file.
  2. Print the resolved value once, with the source of each setting.
  3. Ask only when stdin is a TTY, and always offer --yes.
  4. Fail on an unknown value instead of silently using the default.

Three ways to call the same tool

acme deploy --env staging --dry-run
NO_COLOR=1 acme plan
acme plan --output json | jq -r '.changes[]'
SourceGood for
FlagOne run, CI, an override
EnvironmentMachine-wide defaults
Config fileShared team settings
PromptA human, at a TTY, once

--yes is not a shortcut It is a promise that the caller has read the plan. If the tool cannot print a plan, do not ship --yes.

A prompt in CI is a hang, and a hang is a failed deploy at 3 am.

stdout is data, stderr is prose (the contract)

The shell has one convention worth keeping: data on stdout, commentary on stderr, and exit codes for the result.

CodeMeaning
0The work happened
1The work failed, and the message says why
2You called it wrong
126 or 127Not executable, or not found
128+NKilled by signal N

A script that pipes your output into jq, cut or grep cannot also absorb a progress bar. Write progress to stderr, keep stdout machine-clean, and add --quiet for the humans who want neither.

The same run, read three ways

acme plan --output json > plan.json
acme apply --plan plan.json --yes 2>apply.log
echo "exit $?"

Exit 0 is a claim Never exit 0 after a partial write. The next tool in the pipe treats it as done, and the damage arrives later.

Exit codes are the API that every shell script already knows how to call.

Fail small, fail loud, fail once (failure)

The failure path is the part users meet at the worst moment. Design it with the same care as the happy path.

  • Name the object and the action: plan for acme-api, not an error.
  • Never print a stack trace without a one-line summary above it.
  • Validate before writing, so a bad run leaves no half state.
  • Make the second run safe, because the first fix is usually a retry.

Clean up on the way out

set -euo pipefail
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT INT TERM

Ctrl-C is not an error, it is a request. Catch it, clean up, exit 130, and say what was left untouched.

Partial writes Write to a temporary path and rename into place. rename is atomic on one filesystem, so the file is either old or new, never half.

A user forgives a failure that leaves the system unchanged. They remember one that does not.

Names, versions and docs (the long tail)

People keep a CLI because the tenth use is as easy as the first. That is a naming and documentation problem, not a feature problem.

  • One verb per action, borrowed from tools people already use: get, apply, delete.
  • Version the output format: a new field is fine, a rename is a break.
  • --version prints the version and the build commit on one line.
  • Ship a man page and shell completion; both are read more than the README.
HabitWhy it lasts
Short verbsMuscle memory from git and kubectl
Stable outputScripts pin it and stop breaking
CompletionThe tool teaches its own flags
--versionThe first line of every bug report

Completion is documentation A completion script shows the flags at the moment of use, which is the only moment most people read them.

A CLI people keep is one where the second year costs the same as the first week.

The honest caveat: a good interface cannot rescue a tool that solves the wrong problem, and no amount of --help fixes a command nobody needed.

The lesson: the first ten seconds decide whether there is a second use.

Your turn: Which flag in your tool would you remove first?

More notes: Automation & tooling

Open in the notes app: search, filters, copy This note on GitHub