Skip to content
gohacks.dev
Tooling

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.

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.

Share

Frequently asked questions

How do I handle --option=value in bash?
Match --option=* in the case statement and take the value with ${1#*=}, which removes everything up to the first equals sign.
Why exit with status 2 on bad arguments?
By convention status 2 means incorrect usage, distinguishing it from a runtime failure (1). Many standard tools follow it.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Keep planning

Similar tools