Skip to content
Snippets Groups Projects
Commit 137a5236 authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Import updated utils script (again)

parent e5cd56bf
Branches
No related merge requests found
......@@ -6,7 +6,7 @@
# and checking current user value, and prompts of yes/no user input.
#
# https://git.brandon-rodriguez.com/scripts/bash/utils
# Version 1.1.
# Version 1.2
##
......@@ -65,12 +65,14 @@ function normalize_terminal () {
# Splits into "args", "kwargs", and "flags":
# * Args are any other values that don't match below two formats.
# * Ex: "True", "5", and "test".
# * Flags are any variable that starts with "-".
# * These are effectively treated as booleans. True if present, false otherwise.
# * Ex: "-a", "-b", and "-run".
# * Kwargs are key-value pairs, where the key comes first and starts with "--".
# * Kwargs are key-value pairs, where the key comes first and starts with "--" or "-".
# * Note that the associated value must come immediately after the key.
# * Ex: "--type int", "--dir ./test", and "--name Bob".
# * Flags are any variable that starts with "-" or "--", and must be defined by the calling script.
# * To define flags, populate a "possible_flags" array with flag values prior to calling util script.
# * These are effectively treated as booleans. True if present, false otherwise.
# * Ex: "-a", "--b", "-run", and "--test".
#
# Ordering of provided args, kwargs, and flags should not matter, aside from kwarg keys needing a corresponding value
# as the direct next processed value.
......@@ -111,38 +113,65 @@ handle_args_kwargs () {
# Special handling for help flags.
help_flags=true
# Handle for kwargs.
elif [[ "${arg}" == "--"* && "${arg}" != "---"* ]]
# Handle for flags.
elif [[ ${#possible_flags[@]} > 0 && ${possible_flags[@]} =~ "${arg}" ]]
then
# Check for kwarg handling bool.
if [[ ${handle_kwarg} == true ]]
then
# Expected arg to fill value for key-value pair. Got kwarg key. Raise error.
echo -e "${text_red}Expected value for kwarg key of \"${kwarg_key}\". Got another key of \"${arg}\" instead.${text_reset}"
# Expected arg to fill value for key-value pair. Got flag. Raise error.
echo -e "${text_red}Expected value for kwarg key of \"${kwarg_key}\". Got a flag of \"${arg}\" instead.${text_reset}"
exit 1
else
# Save kwarg key and process next value.
kwarg_key=${arg#--}
handle_kwarg=true
if [[ "${arg}" == "--"* ]]
then
# Handle for double dash flag.
local new_flag=${arg#--}
elif [[ "${arg}" == "-"* ]]
then
# Handle for single dash flag.
local new_flag=${arg#-}
else
# Unexpected kwarg value.
echo -e "${text_red}Unexpected flag type of \"${arg}\". Unable to proceed.${text_reset}"
exit 1
fi
flags+=( ${new_flag} )
# Optionally populate global flags.
if [[ ${set_global_flags} == true ]]
then
global_flags+=( ${new_flag} )
fi
fi
# Handle for flags.
elif [[ "${arg}" == "-"* && "${arg}" != "--"* ]]
# Handle for kwargs.
elif [[ ("${arg}" == "-"* || "${arg}" == "--"*) && "${arg}" != "---"* ]]
then
# Check for kwarg handling bool.
if [[ ${handle_kwarg} == true ]]
then
# Expected arg to fill value for key-value pair. Got flag. Raise error.
echo -e "${text_red}Expected value for kwarg key of \"${kwarg_key}\". Got a flag of \"${arg}\" instead.${text_reset}"
# Expected arg to fill value for key-value pair. Got kwarg key. Raise error.
echo -e "${text_red}Expected value for kwarg key of \"${kwarg_key}\". Got another key of \"${arg}\" instead.${text_reset}"
exit 1
else
# Save kwarg key and process next value.
flags+=( ${arg#-} )
# Optionally populate global flags.
if [[ ${set_global_flags} == true ]]
if [[ "${arg}" == "--"* ]]
then
# Handle for two dash kwarg.
kwarg_key=${arg#--}
handle_kwarg=true
elif [[ "${arg}" == "-"* ]]
then
global_flags+=( ${arg#-} )
# Handle for one dash kwarg.
kwarg_key=${arg#-}
handle_kwarg=true
else
# Unexpected kwarg value.
echo -e "${text_red}Unexpected kwarg key type of \"${arg}\". Unable to proceed.${text_reset}"
exit 1
fi
fi
......@@ -204,11 +233,11 @@ function get_absolute_path () {
# Handle for directory.
# Extra logic to properly handle values of "./" and "../".
local current_dir="$(pwd)"
local current_dir="$(pwd -P)"
cd ${1}
# Then call this to have consistent symlink handling as files.
return_value="$(cd "$(dirname "$(pwd)")"; pwd -P)/$(basename "$(pwd)")"
return_value="$(cd "$(dirname "$(pwd -P)")"; pwd -P)/$(basename "$(pwd -P)")"
# Change back to original location once value is set.
cd ${current_dir}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment