diff --git a/readme.md b/readme.md index fc3ec4a1c825edabba5e69848d5e1c94ccac3057..ccd2b3255784ecdf429518349f036e81259141f3 100644 --- a/readme.md +++ b/readme.md @@ -26,6 +26,20 @@ to keep relative path handling the same, regardless of calling location. Terminal location resets on script end. +### Passed Args +On import, this script splits passed args 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 this logic can be called within functions as well.<br> +Due to how bash works, this will override the original passed script values. These original values will be backed up to +variables called "global_args", "global_kwargs", "global_flags". + ## References See `documents/references.md`. diff --git a/utils.sh b/utils.sh index c833804a7060e693b25d2af8e772b21306af95a6..d7eeb22a5dfc1ee4e6619a45ce7ca63222aa32e3 100755 --- a/utils.sh +++ b/utils.sh @@ -22,9 +22,12 @@ text_yellow="\033[1;33m" text_white="\033[1;37m" # Arg and Kwarg Holder Variables. -args="" -kwargs="" -flags="" +args=() +flags=() +declare -A kwargs=() +global_args=() +global_flags=() +declare -A global_kwargs=() # Function Return Variables. return_value="" @@ -368,11 +371,25 @@ function to_lower () { # * Args are any other values that don't match above formats. ## handle_args_kwargs () { - args=() - flags=() - declare -A 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 ${@} @@ -407,6 +424,12 @@ handle_args_kwargs () { 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 else @@ -418,9 +441,22 @@ handle_args_kwargs () { # 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