Bash getopts Long-Options Parser Prompt
Build a robust Bash option parser that supports both short flags and GNU-style long options with validation and a generated usage block
- Target user
- engineers who automate ops with Bash and Python
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior automation engineer who has shipped dozens of production Bash CLIs and knows the exact boundaries of POSIX getopts versus hand-rolled long-option parsing. I will provide: - The list of options I need (short flag, long name, whether it takes a value, default, required/optional) - The target shell and platform (e.g. bash 4.x on Ubuntu, or POSIX sh on Alpine) - A short description of what the script does, for the help text Your job: 1. **Choose the parsing strategy** — decide between `getopts` (short only) and a `while`/`case` loop that handles `--long`, `--long=value`, and `--` end-of-options; justify the choice for my target shell. 2. **Generate the parser** — emit a complete, copy-pasteable parser block with `set -euo pipefail`, clearly scoped variables, and no global leakage. 3. **Validate inputs** — reject unknown flags, missing required options, and missing values for value-taking flags, each with a distinct exit code and stderr message. 4. **Build the usage block** — produce a `usage()` function whose text is derived from the same option list, so help and parsing never drift apart. 5. **Add a self-test** — include 3-5 example invocations (valid and invalid) plus the expected exit code and output for each. Output as: one fenced bash code block with the parser and usage function, followed by a short table of the example invocations and expected results. Default to failing loudly on unknown or malformed options rather than silently ignoring them, and never let an unparsed flag fall through into positional arguments.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Bash Terminal Color and Styling Library with tput Prompt
Build a reusable Bash output library that uses tput for colors, bold, and status glyphs, auto-disables styling when output is not a TTY or NO_COLOR is set, and degrades gracefully on dumb terminals.
-
Python argparse Subcommand CLI Without Dependencies Prompt
Scaffold a multi-command Python CLI using only the standard-library argparse — subparsers, shared global flags, env-var defaults, and clean exit codes — for scripts that must run with zero pip installs.
-
Bash Script Machine-Readable JSON Output Prompt
Retrofit a bash script with a clean --json output mode using jq, separating human stdout from machine output, emitting valid structured results and proper exit codes so the script composes into pipelines and CI.
-
Python Typer CLI App Scaffold Prompt
Generate a modern, type-hint-driven CLI using Typer — subcommands, validated options, rich help, shell completion, and clean exit codes — for operations tools that need to feel like first-class CLIs.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.