
CLI Help to README — Usage Table From --help
Give it a command's --help output and it writes the README usage table: every command, argument and option with its value, default and required flag. It also lists the options your help text forgot to describe, and --check fails CI when the README table goes stale. #الهندسة
The problem
A README usage section is copied from --help once and then drifts: a flag is added, a default changes, a description is reworded, and the README still shows last month's tool. Writing the table by hand is slow and boring, so it is the first thing to rot. And nobody notices that an option has no description until a user asks what it does.
How it works
- Reads the help text (argparse, click, or GNU style) and finds the
usage:line, even when it wraps. - Sorts what follows into commands, arguments and options, by the section title (
positional arguments,options,Commands, and their variants) or by the shape of the line when there is no title. - For each option it reads the flags (
-o, --output), the value name (PATH), the allowed values ({a,b}or[a|b]), the default ((default: 3)or[default: 3]), and whether the usage line makes it required (outside the square brackets). - Renders Markdown tables and a Gaps list, or JSON.
The result is deterministic: the same help text always gives the same table, so you can commit it and gate it in CI.
Usage
# from a file, or from a pipe
python3 scripts/cli_help_to_readme.py --file help.txt
mytool --help | python3 scripts/cli_help_to_readme.py
# run the command and follow its subcommands (one level by default)
python3 scripts/cli_help_to_readme.py --run "python3 mytool.py"
python3 scripts/cli_help_to_readme.py --run "mytool" --depth 2
# list only what is missing; exit 1 when something is
python3 scripts/cli_help_to_readme.py --run "mytool" --lint
python3 scripts/cli_help_to_readme.py --run "mytool" --lint --strict
# keep the table inside README.md between two markers, then gate it in CI
python3 scripts/cli_help_to_readme.py --run "mytool" --write README.md
python3 scripts/cli_help_to_readme.py --run "mytool" --check README.md
# the parsed result as JSON
python3 scripts/cli_help_to_readme.py --file help.txt --json
All options: --file PATH (- is stdin; stdin is the default), --run COMMAND, --depth N (default 1), --timeout SECONDS (default 10), --json, --lint, --strict, --no-gaps, --write README, --check README.
Example: a file of help text
The package ships a small example command, examples/sample_cli.py (a log filter called logsift), and the help text it printed in examples/sample_help.txt. Running the tool on that file prints exactly this:
$ python3 scripts/cli_help_to_readme.py --file examples/sample_help.txt
## Usage
```text
usage: logsift [-h] [-v] [--color {auto,always,never}] {scan,report} ...
```
Filter and summarize application log files.
### Commands
| Command | Description |
|---|---|
| `scan` | print log lines that match the filters |
| `report` | count lines per level |
### Options
| Option | Value | Default | Required | Description |
|---|---|---|---|---|
| `-h`, `--help` | - | - | no | show this help message and exit |
| `-v`, `--verbose` | - | - | no | print each matching line |
| `--color` | `{auto,always,never}` | `auto` | no | colorize output (default: auto) |
Example: logsift scan app.log --level error
Example: a subcommand, with a gap found
examples/sample_scan_help.txt is the help of logsift scan. One option, --limit, has no description, and the table says so:
$ python3 scripts/cli_help_to_readme.py --file examples/sample_scan_help.txt
## Usage
```text
usage: logsift scan [-h] [--level {debug,info,warn,error}] [--since DURATION] [--limit LIMIT] file
```
### Arguments
| Argument | Required | Description |
|---|---|---|
| `file` | yes | path to the log file |
### Options
| Option | Value | Default | Required | Description |
|---|---|---|---|---|
| `-h`, `--help` | - | - | no | show this help message and exit |
| `--level` | `{debug,info,warn,error}` | `info` | no | lowest level to keep (default: info) |
| `--since` | `DURATION` | - | no | only lines newer than this, such as 90m or 2h |
| `--limit` | `LIMIT` | - | no | - |
### Gaps
- `--limit`: option has no description
--lint prints only the gaps and exits 1, which is what you want in CI:
$ python3 scripts/cli_help_to_readme.py --file examples/sample_scan_help.txt --lint
--limit: option has no description
--strict also counts a value-taking option that states no default, and a help text that shows no example:
$ python3 scripts/cli_help_to_readme.py --file examples/sample_scan_help.txt --lint --strict
--since: option takes a value but states no default
--limit: option has no description
logsift scan: help text shows no example
The README block and --check
--write README.md puts the tables between <!-- cli-usage:start --> and <!-- cli-usage:end -->. If the markers are not there yet it appends them at the end; if they are, it replaces only what is between them and leaves the rest of your README alone. --check README.md passes only when the block is present and equals a fresh run, so changing a flag without regenerating the README fails the build.
Exit codes
| code | meaning |
|---|---|
| 0 | tables printed or written, or --lint found nothing, or --check passed |
| 1 | --lint found gaps, or --check found the block missing or stale |
| 2 | bad input: no help text recognized, the file or the command could not be read |
Limits (read these)
- It reads the words your help prints. If a default is not in the help sentence (
(default: 3)or[default: 3]), the Default column is-; the tool never guesses from the code. --runruns your command with--helpappended (and each subcommand name before it), with no shell, a timeout, and no other arguments. Do not point it at a program whose--helphas side effects. It makes no network calls of its own.- Help text with a very different layout (man pages, tables drawn with box characters) is not recognized and exits 2. Argparse, click and plain GNU-style lists are.
- Positional arguments are listed only when the help prints them under an arguments section.


