Browse Source

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

This reverts commit fb7c410e8a.
master
Olli Graf 1 month ago
parent
commit
89557b8e36
  1. 35
      teil23/parseargs.sh

35
teil23/parseargs.sh

@ -1,20 +1,17 @@
#!/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 <file> Specify a file"
echo " -n, --number <num> Specify a number (default: 42)"
}
# Universelle Parameterparser-Funktion
parse_args() {
POSITIONAL=()
while [[ $# -gt 0 ]]; do
case "${1}" in
case "$1" in
-h|--help)
display_help
echo "Usage: $0 [options]"
echo "Options:"
echo " -h, --help Show this help message"
echo " -v, --verbose Enable verbose mode"
echo " -f, --file <file> Specify a file"
echo " -n, --number <num> Specify a number (default: 42)"
exit 0
;;
-v|--verbose)
@ -22,7 +19,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
@ -30,11 +27,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
@ -56,4 +53,14 @@ 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:-<not set>}"
echo "Number: $NUMBER"
echo "Positional arguments: $@"

Loading…
Cancel
Save