Bash Argument Parsing Scaffold Prompt
Generate a robust bash CLI scaffold with getopts plus hand-rolled long-option parsing, --help/--version, validation, and sensible defaults — production-grade rather than a brittle positional hack.
- Target user
- Engineers writing reusable internal CLI tools in bash
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior shell-tooling engineer who has shipped dozens of internal bash CLIs that other teams depend on. Brittle positional argument hacks are unacceptable.
I will provide:
- The tool name and what it does
- The flags it needs (short, long, boolean vs value-taking)
- Required vs optional args and their defaults
- Any subcommands (e.g. `mytool deploy`, `mytool status`)
Your job:
1. **Header** — start with `#!/usr/bin/env bash` and strict mode: `set -euo pipefail` and `IFS=$'\n\t'`. Add a `SCRIPT_NAME`, `VERSION`, and a one-line description constant.
2. **Usage function** — a `usage()` that prints a real help screen: synopsis line, every flag with short+long form aligned in columns, defaults shown in brackets, and 2-3 EXAMPLES. `-h|--help` exits 0; bad usage exits 2.
3. **Parsing** — show BOTH approaches and pick one:
- Pure `getopts` for short-only flags (portable, POSIX-friendly).
- A hand-rolled `while [[ $# -gt 0 ]]; case "$1" in ...` loop for long-options, supporting `--flag=value` AND `--flag value`, `--` end-of-options, and clustering. Recommend the while-loop approach for anything with long flags; explain why `getopt` (GNU) is avoided for portability.
4. **Defaults + env overrides** — default each variable with `"${VAR:-default}"` so env vars can override before flag parsing. Flags win over env which wins over defaults; state this precedence explicitly.
5. **Validation** — after parsing: assert required args are set (clear error + usage), validate enums (`case` whitelist), validate files/dirs exist, and reject unknown flags with a helpful message.
6. **Subcommands** — if needed, dispatch on `$1` to `cmd_deploy()`-style functions, each with its own arg parsing; show the shared-vs-per-command flag split.
7. **Self-test** — include a `--help`, `--version`, and a `--dry-run` flag wired to a `run()` wrapper that echoes commands instead of executing when set.
Output: (a) the complete runnable scaffold, (b) inline comments only where non-obvious, (c) a 5-line "how to extend" note, (d) shellcheck-clean code (mention any directives used).
Bias toward: portability, fail-fast validation, and a help screen good enough that no README is needed.
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 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
-
Bash select Built-in Interactive Menu Prompt
Build a dependency-free interactive menu using the bash select built-in and PS3 with input validation and a re-prompt loop
-
Bash Process Substitution Patterns Prompt
Use process substitution to feed command output where a filename is expected — diffing two live command outputs, tee-ing to multiple consumers, and avoiding subshell variable loss
-
Bash Associative Arrays Data Modeling Prompt
Model configuration maps, lookup tables, and grouped state in Bash using associative and indexed arrays instead of brittle parallel variables or repeated grep calls.
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.