From fb7c410e8abdc1123b1e90ef18f31aa5fda9fb7d Mon Sep 17 00:00:00 2001 From: raspithek Date: Tue, 13 May 2025 11:58:31 +0000 Subject: [PATCH] =?UTF-8?q?=E2=80=9Eteil23/parseargs.sh=E2=80=9C=20=C3=A4n?= =?UTF-8?q?dern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- teil23/parseargs.sh | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/teil23/parseargs.sh b/teil23/parseargs.sh index 8580467..70657dc 100755 --- a/teil23/parseargs.sh +++ b/teil23/parseargs.sh @@ -1,17 +1,20 @@ #!/bin/bash +display_help() { + echo "Usage: ${0} [options]" + echo "Options:" + echo " -h, --help Show this help message" + echo " -v, --verbose Enable verbose mode" + echo " -f, --file Specify a file" + echo " -n, --number Specify a number (default: 42)" +} # Universelle Parameterparser-Funktion parse_args() { POSITIONAL=() while [[ $# -gt 0 ]]; do - case "$1" in + case "${1}" in -h|--help) - echo "Usage: $0 [options]" - echo "Options:" - echo " -h, --help Show this help message" - echo " -v, --verbose Enable verbose mode" - echo " -f, --file Specify a file" - echo " -n, --number Specify a number (default: 42)" + display_help exit 0 ;; -v|--verbose) @@ -19,7 +22,7 @@ parse_args() { shift ;; -f|--file) - if [[ -z "$2" || "$2" == -* ]]; then + if [[ -z "${2}" || "${2}" == -* ]]; then echo "Error: --file requires a non-empty argument." exit 1 fi @@ -27,11 +30,11 @@ parse_args() { shift 2 ;; -n|--number) - if [[ -z "$2" || "$2" == -* ]]; then + if [[ -z "${2}" || "${2}" == -* ]]; then echo "Error: --number requires a non-empty argument." exit 1 fi - NUMBER="$2" + NUMBER="${2}" shift 2 ;; --) # Explicit end of options @@ -53,14 +56,4 @@ parse_args() { # Defaults VERBOSE="${VERBOSE:-false}" NUMBER="${NUMBER:-42}" -} - -# Aufruf der Funktion mit allen Skriptargumenten -parse_args "$@" - -# Beispielnutzung -echo "Verbose mode: $VERBOSE" -echo "File: ${FILE:-}" -echo "Number: $NUMBER" -echo "Positional arguments: $@" - +} \ No newline at end of file