Every script answers a help flag and does nothing else
One of my scripts took its help flag as a hostname and disabled Apache. The one line that now sits at the top of all 45, in bash and in Python.
I have a script that brings one of my Linux boxes into line with the other. It takes the source machine as its first argument, pulls that machine’s package list, config files and enabled services over ssh, and makes the box it’s running on match. It needs root for most of that. It had no --help.
Then it got run with --help, by a coding agent that was checking my build guide against the real machines.
What it did
The script took --help as the name of the source host and started work.
Every rsync failed on the bad host name, so no file was overwritten. The ssh calls that ask the source what it has enabled failed too, and they failed quietly: each one came back as an empty list. The Apache step compares what’s enabled locally against that list and disables the difference. Against an empty list the difference is everything, so it disabled all 26 modules, 8 confs and 12 sites, and then stopped with “No MPM loaded”. The same run disabled a systemd timer, ran locale-gen and apt-get update, and deleted Chrome’s lock files while Chrome was open.
Apache carried on serving from memory, so nothing was down. I had a snapshot of the machine’s state from 46 minutes earlier, which included the three lists, and putting it back was a2enmod, a2enconf and a2ensite over those and apache2ctl -t: Syntax OK. A fresh snapshot matched the earlier one.
The line
Every bash script of mine now has this directly under its header comment, before set -u and before anything else:
case ${1:-} in -h|--help) sed -n '2,/^[^#]/{/^#/p}' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; esac
It prints the script’s own comment block, from line 2 to the first line that isn’t a comment, with the # stripped, and exits 0. The help text is the header, so there’s only one of them to keep right. With a header like this:
#!/usr/bin/env bash
# rotate-logs: compress yesterday's logs in /var/log/myapp and delete those older than 30 days.
#
# rotate-logs do it
# rotate-logs --dry-run list what would be compressed and deleted
case ${1:-} in -h|--help) sed -n '2,/^[^#]/{/^#/p}' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; esac
set -eu
you get:
$ rotate-logs --help
rotate-logs: compress yesterday's logs in /var/log/myapp and delete those older than 30 days.
rotate-logs do it
rotate-logs --dry-run list what would be compressed and deleted
The Python ones print the module docstring. Where a script doesn’t use argparse, this is the first thing in main(), before any argument is looked at:
if sys.argv[1:2] in (["-h"], ["--help"]):
print(__doc__)
raise SystemExit(0)
The repo has 45 executable scripts, 31 in bash and 14 in Python. The Python ones already answered --help. The line went into 25 other bash scripts in the same commit that fixed the first one.
The script itself
The one that caused it got more than a help option. It now refuses, before it does anything:
- an option it doesn’t know
- a part name it doesn’t know (it runs in named parts: packages, snaps, services and so on)
- a source that doesn’t answer over ssh
And the Apache step does nothing when the source’s list comes back empty. That was the real bug. A help option would have stopped this run, but a typo in a real host name would have done exactly the same damage.
The other half
A help option only protects a script that has one. So there’s a second rule, written into the rules my coding agents work under: never run a script to find out whether it takes --help. Read its header first, with sed -n 1,40p.
Featured projects
-
Apple News API
Publish articles and live coverage to Apple News from PHP, with the whole Apple News Format built from objects.
-
GitHub Auditor
Health, access and activity reports for more GitHub and Bitbucket repositories than you can keep in your head.
-
PrintMD
Markdown in, and a designed PDF, Word or PowerPoint file out. Its PDF service runs on your own computer.
-
Where Should I Live
Which city would leave you best off? Cities ranked by take-home pay, living costs, visas and the way you live.
-
Solar Calendar
Thirteen months of exactly four weeks, with the year starting on the first Monday on or after the spring equinox.
Latest writing
-
Many sessions at once, and what still needs me
Fifteen coding-agent sessions across two Linux boxes, one session that watches the rest, what it may answer for me, what it never may, and why a yes it passes on gets checked against what I typed.
-
61 public repos of agent tooling, none installed, six ideas kept
One author, 61 public repositories of coding-agent tooling: what I checked before any of it got near my Linux boxes, why none of it was installed, and the six ideas I built my own way.
-
Rules a coding agent cannot talk its way round
The hook that reads every command a coding agent wants to run on my Linux boxes: what it refuses, the 27 proofs that its tests fail when a rule is taken out, and the four lines every report opens with.
-
Dozens of repos, one person
46 repositories and two Linux boxes with nobody else on them: the diary, the decision table, the rebuild guide and the job that keeps both sets of clones in step.