diff options
| author | Stefan Kreutz <mail@skreutz.com> | 2026-04-15 23:22:57 +0200 |
|---|---|---|
| committer | Stefan Kreutz <mail@skreutz.com> | 2026-04-16 00:41:56 +0200 |
| commit | b1735044d222e7173447d6b72d6bc90085994d32 (patch) | |
| tree | 52b8494020af10496f776b028b1894ad0fd9bfbd /temp-postgres.sh | |
| parent | 2a7d8d3aa4b8b7e345cda70ea0a110bef775d132 (diff) | |
| download | temp-postgres-b1735044d222e7173447d6b72d6bc90085994d32.tar.gz | |
Turn required arguments into optional flags
Thereby add minimal --help option.
This is a breaking change of the command-line interface.
Diffstat (limited to 'temp-postgres.sh')
| -rwxr-xr-x | temp-postgres.sh | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/temp-postgres.sh b/temp-postgres.sh index 15e169b..6064b66 100755 --- a/temp-postgres.sh +++ b/temp-postgres.sh @@ -1,6 +1,6 @@ #! /bin/sh -# Copyright (c) 2019, 2020, 2022 Stefan Kreutz <mail@skreutz.com> +# Copyright (c) 2019, 2020, 2022, 2026 Stefan Kreutz <mail@skreutz.com> # # Permission to use, copy, modify, and distribute this software for any purpose # with or without fee is hereby granted, provided that the above copyright @@ -14,7 +14,7 @@ # OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR # PERFORMANCE OF THIS SOFTWARE. -set -u +set -o nounset # Remove the temporary directory before exiting. trap 'quit' INT @@ -30,14 +30,40 @@ quit() { exit "${code}" } -# Parse arguments. -[ $# -eq 2 ] || { - ( >&2 printf "temp-postgres: invalid arguments\\n" ) - printf "Usage: temp-postgres <dbname> <username>\\n" - quit 1 -} -dbname="$1" -username="$2" +dbname="$( id -un )" +username="$( id -un )" + +while [ $# -gt 0 ] ; do + case "$1" in + -h|--help) + echo "Usage: temp-postgres [-h|--help] [-d|--dbname <dbname>] [-u|--username <username>]" + exit + ;; + -d|--dbname) + dbname="$2" + shift 2 + ;; + -u|--username) + username="$2" + shift 2 + ;; + --) + shift + break + ;; + -*) + ( >&2 echo "undefined option: $1" ) + exit 2 + ;; + *) + break + ;; + esac +done +if [ $# -gt 0 ] ; then + ( >&2 echo "too many arguments" ) + exit 2 +fi # Create a temporary directory tmpdir="$( mktemp -d )" || { |