Fixes long and short command line options.

main
Yasen Pramatarov 2024-06-08 17:05:28 +03:00
parent 98e7c7d2b6
commit 3597a01997
1 changed files with 43 additions and 10 deletions

53
jilo
View File

@ -309,33 +309,66 @@ is_process_running() {
pgrep -f "$1" >/dev/null 2>&1 pgrep -f "$1" >/dev/null 2>&1
} }
# commandline options ### Commandline options
while getopts ":dfcpv" opt; do verbose=false
case $opt in
d) # Expand combined multiple short options
expand_options() {
local arg="$1"
local expanded=""
local i
for ((i=1; i<${#arg}; i++)); do
expanded="${expanded} -${arg:i:1}"
done
echo "$expanded"
}
# We try to distinguish short ("-o") and long ("--option") options
args=()
while [[ $# -gt 0 ]]; do
case "$1" in
# only one dash, could be short option
-[!-]?*)
args+=("$(expand_options "$1")")
;;
# all other cases, including long option
*)
args+=("$1")
;;
esac
shift
done
# switch between the options
for arg in "${args[@]}"; do
case "$arg" in
-d | --create-db)
cmd="--create-db" cmd="--create-db"
;; ;;
f) -f | --flush)
cmd="--flush" cmd="--flush"
;; ;;
c) -c | --check)
cmd="--check" cmd="--check"
;; ;;
p) -p | --parse)
cmd="--parse" cmd="--parse"
;; ;;
v) -v | --verbose)
verbose=true verbose=true
;; ;;
\?) -h | --help)
echo -e "$help"
exit 0
;;
*)
echo "Invalid option: -$OPTARG" >&2 echo "Invalid option: -$OPTARG" >&2
echo -e "$help" echo -e "$help"
exit 1 exit 1
;; ;;
esac esac
done done
shift $((OPTIND -1))
case "$cmd" in case "$cmd" in