From b1735044d222e7173447d6b72d6bc90085994d32 Mon Sep 17 00:00:00 2001 From: Stefan Kreutz Date: Wed, 15 Apr 2026 23:22:57 +0200 Subject: Turn required arguments into optional flags Thereby add minimal --help option. This is a breaking change of the command-line interface. --- temp-postgres.sh | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'temp-postgres.sh') 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 +# Copyright (c) 2019, 2020, 2022, 2026 Stefan Kreutz # # 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 \\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 ] [-u|--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 )" || { -- cgit v1.3