Bash Argument Parser Generator
Generate argument parsing for a bash script with short and long options, values, a usage message, -- handling and errors for unknown flags.
Free · No sign-up · Your inputs stay in your browser
Input
Generate argument parsing for a bash script
Output
deploy.sh
1,123 characters
A while/case loop rather than getopts, because getopts cannot parse --long options and GNU getopt behaves differently on macOS. It handles --opt value, --opt=value and -- to end options, and exits with 2, the convention for usage errors. set -euo pipefail stops the script at the first failing command, unset variable or failing pipe.
Runs in this browser. Nothing you paste is sent to a server.
About this tool
Method, assumptions, practical next steps, and limits.
Why not getopts
The getopts builtin is portable but only parses single-letter options, so --dry-run is out of reach. GNU getopt supports long options, but the version on macOS is the old BSD one with different behaviour. A plain while loop over $@, as generated here, works the same everywhere bash does.
The strict mode line
set -e exits on the first failing command, -u treats unset variables as errors, and -o pipefail makes a pipeline fail when any part fails rather than only the last. Together they turn silent failures into visible ones. The set builtin documentation lists each option.