From 30675230eb47a14f75171be08935edb8963029db Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Sat, 10 Oct 2020 16:56:29 -0400
Subject: [PATCH] Improve global handling of args/kwargs/flags

---
 readme.md | 14 ++++++++++++++
 utils.sh  | 48 ++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/readme.md b/readme.md
index fc3ec4a..ccd2b32 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 c833804..d7eeb22 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
 
-- 
GitLab