accept both args and flags

This commit is contained in:
Pocco81 2021-08-03 23:09:18 -05:00
parent bbbd73fc56
commit acceccda87
1 changed files with 46 additions and 127 deletions

View File

@ -30,75 +30,31 @@ prompt() {
_usage() { _usage() {
printf "%s" \ printf "%s" \
"Usage: sh ${0##*/} [ options ] "Usage: sh ${0##*/} [ options ]
-h | --help => Show this help. -h, --help -> Show this help.
-i | --install => Install the config. -i, --install -> Install the config.
-r | --remove => Remove the config. -r, --remove -> Remove the config.
-u | --update => Update the existing config without removing existing stuff. -u, --update -> Update the existing config without removing existing stuff.
" "
} }
_eval_exit() { skip=0
status=$1
suc_msg=$2
err_msg=$3
case $status in
0)
prompt -s "${suc_msg}"
;;
*)
prompt -e "${err_msg}"
;;
esac
_skip_ahead() {
amount=$1
skip=$((skip + amount))
} }
_check_dependencies() {
_error_dependencies() {
prompt -r "Error: Install ${1} before proceeding."
exit 1
}
command -v git 1>/dev/null || _error_dependencies git
return 0
}
_install() {
_check_dependencies
}
_remove() {
prompt -i "-> Cleaning config ($HOME/.config/nvim/)"
# rm -rf "$HOME/.config/nvim/"
prompt -i "-> Cleaning miscellaneous ($HOME/.local/share/nvim/)"
# rm -rf "$HOME/.local/share/nvim/"
prompt -i "-> Cleaning cache ($HOME/.cache/nvim/)"
# rm -rf "$HOME/.cache/nvim/"
}
# _update() {}
_init_settings() {
no_backup="false"
install="false"
uninstall="false"
}
_clean_arg() {
arg=$1
if [[ "$arg" == "--"* ]]; then
echo "${arg:2}"
elif [[ "$arg" == "-"* ]]; then
echo "${arg:1}"
fi
}
_parse_args() { _parse_args() {
local args_func=$1 local func_args=$1
local argv=("$@") local argv=("$@")
# local skip=0
unset 'argv[0]' # becuase arg1 is $func_arg
for i in "${!argv[@]}"; do new_array+=( "${argv[i]}" ); done
argv=("${new_array[@]}")
unset new_array
local argc=${#argv[@]} local argc=${#argv[@]}
local skip=0
_skip_ahead() { _skip_ahead() {
amount=$1 amount=$1
@ -129,99 +85,62 @@ _parse_args() {
break break
;; ;;
*) *)
eval "${eval_args}" "$(_clean_arg "${argv[j]}")" "$j" # eval "${func_args}" "$(_clean_arg "${argv[j]}")" "$j"
eval "${func_args}" "${argv[j]}" "$j"
;; ;;
esac esac
;; ;;
-*) -*)
tangled_args=$(_clean_arg "${argv[j]}") if [[ ${#argv[j]} -le 2 ]]; then
for ((k = 0; k < ${#tangled_args}; k++)); do eval "${func_args}" "${argv[j]}" "$j"
eval "${eval_args}" "${tangled_args:$k:1}" "$j" else
done tangled_args=$(_clean_arg "${argv[j]}")
for ((k = 0; k < ${#tangled_args}; k++)); do
eval "${func_args}" "-${tangled_args:$k:1}" "$j"
done
fi
;; ;;
*) *)
prompt -w "Warning: flag ''${argv[j]}' not recognized" eval "${func_args}" "${argv[j]}" "$j"
;; ;;
esac esac
done done
} }
main() { main() {
local argv=("$@") local argvs=("$@")
local argc=${#argv[@]} local argc=${#argvs[@]}
local skip=0
assert_arg() { assert_arg() {
var=$1 var=$1 # flag
index=$2 index=$2 # flag's index
case ${var} in case ${var} in
h | help) -h | --help)
_usage _usage
;; ;;
i | install) -i | --install)
prompt -i "installing..." prompt -i "installing..."
;; ;;
r | remove) -r | --remove)
prompt -i "removing..." prompt -i "removing..."
;; ;;
# p | --path -a | --action)
action=${argvs[index+1]}
prompt -i "Action to perform -> ${action}"
_skip_ahead 1
;;
-p=* | --path=*)
path="${var#*=}"
prompt -i "Path was set to -> ${path}"
;;
*) *)
prompt -w "Warning: flag '${var}' not recognized" prompt -w "Warning: unknown command '${var}'"
;; ;;
esac esac
} }
_skip_ahead() { _parse_args "assert_arg" "${argvs[@]}"
amount=$1
skip=$((skip + amount))
}
# works for:
# 1. normal flags (e.g. -h,--help)
# 2. nested flags (e.g. -ivh)
# 3. space sperated flags and args (e.g. --option argument)
# 4. equal separated flags and args (e.g. --option=argument)
for j in "${!argv[@]}"; do
if [[ ${skip} -gt 0 ]]; then
left=$((argc - j))
while [[ ${skip} > ${left} ]]; do ((skip--)); done
skip=$((skip - 1))
continue
fi
case ${argv[j]} in
--*) # End of all options.
case ${argv[j]} in
--) # End of all options.
break
;;
*)
assert_arg "$(_clean_arg "${argv[j]}")" "$j"
;;
esac
;;
-*)
tangled_args=$(_clean_arg "${argv[j]}")
for ((k = 0; k < ${#tangled_args}; k++)); do
assert_arg "${tangled_args:$k:1}" "$j"
done
;;
*)
prompt -w "Warning: flag ''${argv[j]}' not recognized"
;;
esac
done
} }
init() {
if [ $# -eq 0 ]; then
prompt -e "ERROR: This script needs at least one argument"
else
_init_settings
main "${@}"
fi
}
init "${@}" main "${@}"