Revert "„teil23/parseargs.sh“ ändern"

This reverts commit fb7c410e8a.
This commit is contained in:
2025-05-17 12:09:46 +02:00
parent 5d830cbae4
commit 89557b8e36

View File

@@ -1,20 +1,17 @@
#!/bin/bash #!/bin/bash
display_help() { # Universelle Parameterparser-Funktion
echo "Usage: ${0} [options]" parse_args() {
POSITIONAL=()
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
echo "Usage: $0 [options]"
echo "Options:" echo "Options:"
echo " -h, --help Show this help message" echo " -h, --help Show this help message"
echo " -v, --verbose Enable verbose mode" echo " -v, --verbose Enable verbose mode"
echo " -f, --file <file> Specify a file" echo " -f, --file <file> Specify a file"
echo " -n, --number <num> Specify a number (default: 42)" echo " -n, --number <num> Specify a number (default: 42)"
}
# Universelle Parameterparser-Funktion
parse_args() {
POSITIONAL=()
while [[ $# -gt 0 ]]; do
case "${1}" in
-h|--help)
display_help
exit 0 exit 0
;; ;;
-v|--verbose) -v|--verbose)
@@ -22,7 +19,7 @@ parse_args() {
shift shift
;; ;;
-f|--file) -f|--file)
if [[ -z "${2}" || "${2}" == -* ]]; then if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: --file requires a non-empty argument." echo "Error: --file requires a non-empty argument."
exit 1 exit 1
fi fi
@@ -30,11 +27,11 @@ parse_args() {
shift 2 shift 2
;; ;;
-n|--number) -n|--number)
if [[ -z "${2}" || "${2}" == -* ]]; then if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: --number requires a non-empty argument." echo "Error: --number requires a non-empty argument."
exit 1 exit 1
fi fi
NUMBER="${2}" NUMBER="$2"
shift 2 shift 2
;; ;;
--) # Explicit end of options --) # Explicit end of options
@@ -57,3 +54,13 @@ parse_args() {
VERBOSE="${VERBOSE:-false}" VERBOSE="${VERBOSE:-false}"
NUMBER="${NUMBER:-42}" NUMBER="${NUMBER:-42}"
} }
# Aufruf der Funktion mit allen Skriptargumenten
parse_args "$@"
# Beispielnutzung
echo "Verbose mode: $VERBOSE"
echo "File: ${FILE:-<not set>}"
echo "Number: $NUMBER"
echo "Positional arguments: $@"