Compare commits
3 Commits
1.4
...
5d830cbae4
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d830cbae4 | |||
| fb7c410e8a | |||
| e4190bae83 |
59
teil23/parseargs.sh
Executable file
59
teil23/parseargs.sh
Executable file
@@ -0,0 +1,59 @@
|
|||||||
|
#!/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
|
||||||
|
-h|--help)
|
||||||
|
display_help
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-v|--verbose)
|
||||||
|
VERBOSE=true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-f|--file)
|
||||||
|
if [[ -z "${2}" || "${2}" == -* ]]; then
|
||||||
|
echo "Error: --file requires a non-empty argument."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
FILE="$2"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-n|--number)
|
||||||
|
if [[ -z "${2}" || "${2}" == -* ]]; then
|
||||||
|
echo "Error: --number requires a non-empty argument."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
NUMBER="${2}"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
--) # Explicit end of options
|
||||||
|
shift
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
-*) # Unknown option
|
||||||
|
echo "Error: Unknown option $1"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*) # Positional argument
|
||||||
|
POSITIONAL+=("$1")
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
set -- "${POSITIONAL[@]}" # Restore positional parameters
|
||||||
|
|
||||||
|
# Defaults
|
||||||
|
VERBOSE="${VERBOSE:-false}"
|
||||||
|
NUMBER="${NUMBER:-42}"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user