basic parser

This commit is contained in:
Pocco81 2021-08-03 22:38:03 -05:00
parent 668f1d5623
commit bbbd73fc56

View file

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/bash
RED='\033[0;31m' RED='\033[0;31m'
GREEN='\033[0;32m' GREEN='\033[0;32m'
@ -35,11 +35,10 @@ _usage() {
-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.
" "
exit 0
} }
_eval_exit() { _eval_exit() {
status=$1 status=$1
suc_msg=$2 suc_msg=$2
err_msg=$3 err_msg=$3
@ -54,43 +53,173 @@ _eval_exit() {
} }
# _install() {} _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() { _remove() {
prompt -i "-> Cleaning '$HOME/.config/nvim/'" prompt -i "-> Cleaning config ($HOME/.config/nvim/)"
# rm -rf "$HOME/.config/nvim/" # rm -rf "$HOME/.config/nvim/"
prompt -i "-> Cleaning '$HOME/.local/share/nvim/'" prompt -i "-> Cleaning miscellaneous ($HOME/.local/share/nvim/)"
# rm -rf "$HOME./local/share/nvim/" # rm -rf "$HOME/.local/share/nvim/"
prompt -i "-> Cleaning cache ($HOME/.cache/nvim/)"
# rm -rf "$HOME/.cache/nvim/"
} }
# _update() {} # _update() {}
main() { _init_settings() {
while :; do no_backup="false"
case $1 in install="false"
-h | -\? | --help) uninstall="false"
_usage }
exit 0
_clean_arg() {
arg=$1
if [[ "$arg" == "--"* ]]; then
echo "${arg:2}"
elif [[ "$arg" == "-"* ]]; then
echo "${arg:1}"
fi
}
_parse_args() {
local args_func=$1
local argv=("$@")
local argc=${#argv[@]}
local skip=0
_skip_ahead() {
amount=$1
skip=$((skip + amount))
}
_clean_arg() {
arg=$1
if [[ "$arg" == "--"* ]]; then
echo "${arg:2}"
elif [[ "$arg" == "-"* ]]; then
echo "${arg:1}"
fi
}
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
;;
*)
eval "${eval_args}" "$(_clean_arg "${argv[j]}")" "$j"
;;
esac
;; ;;
-i | --install) -*)
_install tangled_args=$(_clean_arg "${argv[j]}")
for ((k = 0; k < ${#tangled_args}; k++)); do
eval "${eval_args}" "${tangled_args:$k:1}" "$j"
done
;; ;;
-r | --remove) *)
_remove prompt -w "Warning: flag ''${argv[j]}' not recognized"
_eval_exit $? "Successfully removeed NvChad!" "Failed to remove NvChad"
;;
*) # Default case: No more options, so break out of the loop.
break
;; ;;
esac esac
shift
done done
}
main() {
local argv=("$@")
local argc=${#argv[@]}
local skip=0
assert_arg() {
var=$1
index=$2
case ${var} in
h | help)
_usage
;;
i | install)
prompt -i "installing..."
;;
r | remove)
prompt -i "removing..."
;;
# p | --path
*)
prompt -w "Warning: flag '${var}' not recognized"
;;
esac
}
_skip_ahead() {
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() { init() {
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
prompt -e "ERROR: This script needs at least one argument" prompt -e "ERROR: This script needs at least one argument"
else else
_init_settings
main "${@}" main "${@}"
fi fi
} }