#!/usr/bin/env bash # Writes the CLI reference page from `idx --help` output, walking every # subcommand. `nix flake check` runs it against the packaged binary and fails # when the committed page is out of date. # # web/scripts/gen-cli-reference.sh > web/src/content/docs/cli/reference.md set -euo pipefail idx=${1:?usage: gen-cli-reference.sh } subcommands() { "$idx" "$@" --help | awk ' /^Commands:/ { in_cmds = 1; next } in_cmds && /^[^ ]/ { exit } in_cmds && /^ [a-z]/ { if ($1 != "help") print $1 } ' } section() { local depth=$1 shift local hashes hashes=$(printf '#%.0s' $(seq 1 "$depth")) # shellcheck disable=SC2016 # backticks are Markdown, not command substitution printf '%s `idx %s`\n\n```text\n' "$hashes" "$*" "$idx" "$@" --help printf '```\n\n' local sub for sub in $(subcommands "$@"); do section $((depth + 1)) "$@" "$sub" done } main() { cat <<'EOF' --- title: Command reference description: Every idx command and flag, generated from idx --help. --- This page is generated from `idx --help`, so it always matches the released CLI. For explanations and examples, see the [stocks](/cli/stocks/) and [ownership](/cli/ownership/) guides. ## `idx` ```text EOF "$idx" --help printf '```\n\n' for group in $(subcommands); do section 2 "$group" done } main | sed -e 's/[[:space:]]*$//' -e '${/^$/d}'