diff --git a/utils.sh b/utils.sh index c07404bf6bb0e33b2ad2f76114476dad3614cded..968c1e387047a101672c72fa6e1c7af27d65d86c 100755 --- a/utils.sh +++ b/utils.sh @@ -40,7 +40,7 @@ file_extension="" #endregion Global Utility Variables -#region Directory Functions +#region Script Setup Functions ### # Normalizes the location of the terminal to directory of utils.sh file. @@ -56,6 +56,121 @@ function normalize_terminal () { } +### + # Handles passed values. + # Splits into "args", "kwargs", and "flags": + # * 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 "--". + # * Note that the associated value must come immediately after the key. + # * Ex: "--type false", "--dir ./test", and "--name Bob". + # * Args are any other values that don't match above formats. + # + # Note that "-h" and "--help" are handled separately, and considered to both be "help flags". + # If either one is provided, then a "help_flags" global variable is set to true. + # The expectation is that some kind of "display help" function will run instead of normal script functionality. + ## +handle_args_kwargs () { + local handle_kwarg=false + local kwarg_key="" + set_global_args=false + set_global_flags=false + set_global_kwargs=false + + # On first run, set "global" arg/kwarg values, as backup. + # Useful in case any functions ever call this for additional arg/kwarg/flag handling. + # Prevents original passed values from being overriden by a function's passed values. + if [[ ${#global_args[@]} == 0 && ${#global_flags[@]} == 0 && ${#global_kwargs[@]} == 0 ]] + then + set_global_args=true + set_global_flags=true + set_global_kwargs=true + else + args=() + flags=() + kwargs=() + fi + + # Parse all args. + for arg in ${@} + do + # Check arg type, based on substring match. + if [[ "${arg}" == "-h" || "${arg}" == "--help" ]] + then + # Special handling for help flags. + help_flags=true + + # Handle for kwargs. + elif [[ "${arg}" == "--"* && "${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}" + exit 1 + else + # Save kwarg key and process next value. + kwarg_key=${arg#--} + handle_kwarg=true + fi + + # Handle for flags. + elif [[ "${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}" + exit 1 + else + # Save kwarg key and process next value. + flags+=( ${arg#-} ) + + # Optionally populate global flags. + if [[ ${set_global_flags} == true ]] + then + global_flags+=( ${arg#-} ) + fi + fi + + # Handle for args. + else + # Check for kwarg handling bool. + if [[ ${handle_kwarg} == true ]] + then + # Set key-value kwarg pair. + kwargs[${kwarg_key}]=${arg} + handle_kwarg=false + + # Optionally populate global kwargs. + if [[ ${set_global_kwargs} == true ]] + then + global_kwargs[${kwarg_key}]=${arg} + fi + + else + # Add arg to list of args. + args+=( ${arg} ) + + # Optionally populate global args. + if [[ ${set_global_args} == true ]] + then + global_args+=( ${arg} ) + fi + fi + fi + + done +} + +#endregion Script Setup Functions + + +#region Directory Functions + ### # Gets absolute path of passed location. ## @@ -362,118 +477,7 @@ function to_lower () { #endregion Text Manipulation Functions -#region Other Functions - -### - # Handles passed values. - # Splits into "args", "kwargs", and "flags": - # * 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 "--". - # * Note that the associated value must come immediately after the key. - # * Ex: "--type false", "--dir ./test", and "--name Bob". - # * Args are any other values that don't match above formats. - # - # Note that "-h" and "--help" are handled separately, and considered to both be "help flags". - # If either one is provided, then a "help_flags" global variable is set to true. - # The expectation is that some kind of "display help" function will run instead of normal script functionality. - ## -handle_args_kwargs () { - local handle_kwarg=false - local kwarg_key="" - set_global_args=false - set_global_flags=false - set_global_kwargs=false - - # On first run, set "global" arg/kwarg values, as backup. - # Useful in case any functions ever call this for additional arg/kwarg/flag handling. - # Prevents original passed values from being overriden by a function's passed values. - if [[ ${#global_args[@]} == 0 && ${#global_flags[@]} == 0 && ${#global_kwargs[@]} == 0 ]] - then - set_global_args=true - set_global_flags=true - set_global_kwargs=true - else - args=() - flags=() - kwargs=() - fi - - # Parse all args. - for arg in ${@} - do - # Check arg type, based on substring match. - if [[ "${arg}" == "-h" || "${arg}" == "--help" ]] - then - # Special handling for help flags. - help_flags=true - - # Handle for kwargs. - elif [[ "${arg}" == "--"* && "${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}" - exit 1 - else - # Save kwarg key and process next value. - kwarg_key=${arg#--} - handle_kwarg=true - fi - - # Handle for flags. - elif [[ "${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}" - exit 1 - else - # Save kwarg key and process next value. - flags+=( ${arg#-} ) - - # Optionally populate global flags. - if [[ ${set_global_flags} == true ]] - then - global_flags+=( ${arg#-} ) - fi - fi - - # Handle for args. - else - # Check for kwarg handling bool. - if [[ ${handle_kwarg} == true ]] - then - # Set key-value kwarg pair. - kwargs[${kwarg_key}]=${arg} - handle_kwarg=false - - # Optionally populate global kwargs. - if [[ ${set_global_kwargs} == true ]] - then - global_kwargs[${kwarg_key}]=${arg} - fi - - else - # Add arg to list of args. - args+=( ${arg} ) - - # Optionally populate global args. - if [[ ${set_global_args} == true ]] - then - global_args+=( ${arg} ) - fi - fi - fi - - done -} - +#region Display Functions ### # Prints out all available text colors provided by this script. @@ -579,7 +583,7 @@ function display_args_kwargs () { echo "" } -#endregion Other Functions +#endregion Display Functions # Functions to call on script import.