Help, usage errors and dry-run modes
A professional CLI explains its accepted syntax with --help, provides a version with --version, and returns useful status codes. For this lesson, help and version return zero without requiring operands; invalid usage returns 2, and operation failures return 1. A dry run means describing intended changes without performing them, including no temporary mutation of the target. It is not proof that the real operation will succeed. Validate arguments and distinguish preview output from actual completion. For state-changing operations, require an explicit apply option or a deliberate confirmation according to the contract; do not make automated callers wait for an unexpected interactive prompt. Shell completion is a convenience for interactive users, not validation: suggest known flags and still validate in the parser. The informational example accepts exactly one option, so extra operands are rejected rather than ignored. For the apply example, noclobber is a limited safeguard for existing regular files, not a general secure creation protocol.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Help and version without side effects
report() { if (( $# != 1 )); then printf 'invalid usage\n' >&2; return 2 fi case ${1-} in --help) printf 'usage: report --help | --version\n' ;; --version) printf 'report 1.0.0\n' ;; *) printf 'invalid usage\n' >&2; return 2 ;; esac}report --help || exit 1report --versionExpected stdout:
usage: report --help | --versionreport 1.0.0The version is fictional application metadata, not the Bash version. Both calls succeed. The function checks that exactly one option was supplied.
Preview before explicit apply
report() { if (( $# != 1 )); then return 2; fi case $1 in --dry-run) printf 'would create report.txt\n' ;; --apply) if (set -C; printf 'ready\n' > report.txt); then printf 'created report.txt\n' else return 1; fi ;; *) printf 'invalid usage\n' >&2; return 2 ;; esac}report --dry-run[[ ! -e report.txt ]] || exit 1report --apply || exit 1cat report.txtExpected stdout:
would create report.txtcreated report.txtreadyThe preview creates nothing. Explicit apply writes a fixed fixture file using noclobber; it is not a general secure file-creation protocol.
Check usage failure
report() { if [[ $# != 1 || $1 != --dry-run ]]; then printf 'usage: report --dry-run\n' >&2 return 2 fi printf 'would create report.txt\n'}report --unknownstatus=$?printf 'status=%s\n' "$status"Expected stdout:
status=2Expected stderr:
usage: report --dry-runInvalid usage produces a diagnostic and no planned action.
Common mistakes
A dry run that creates and then deletes the target has still changed state. Never promise that a preflight check prevents every race or that confirmation replaces validation.
Practice
Build a fixture-only report command with --help, --version, --dry-run and --apply. Verify that help and dry run create no file, that explicit apply writes the expected report, and that an unknown option leaves the file unchanged.
Next: Configuration precedence and CLI testing