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

Improve global handling of args/kwargs/flags

parent ec598d0f
Branches
No related merge requests found
...@@ -26,6 +26,20 @@ to keep relative path handling the same, regardless of calling location. ...@@ -26,6 +26,20 @@ to keep relative path handling the same, regardless of calling location.
Terminal location resets on script end. 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 ## References
See `documents/references.md`. See `documents/references.md`.
...@@ -22,9 +22,12 @@ text_yellow="\033[1;33m" ...@@ -22,9 +22,12 @@ text_yellow="\033[1;33m"
text_white="\033[1;37m" text_white="\033[1;37m"
# Arg and Kwarg Holder Variables. # Arg and Kwarg Holder Variables.
args="" args=()
kwargs="" flags=()
flags="" declare -A kwargs=()
global_args=()
global_flags=()
declare -A global_kwargs=()
# Function Return Variables. # Function Return Variables.
return_value="" return_value=""
...@@ -368,11 +371,25 @@ function to_lower () { ...@@ -368,11 +371,25 @@ function to_lower () {
# * Args are any other values that don't match above formats. # * Args are any other values that don't match above formats.
## ##
handle_args_kwargs () { handle_args_kwargs () {
args=()
flags=()
declare -A kwargs
local handle_kwarg=false local handle_kwarg=false
local kwarg_key="" 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. # Parse all args.
for arg in ${@} for arg in ${@}
...@@ -407,6 +424,12 @@ handle_args_kwargs () { ...@@ -407,6 +424,12 @@ handle_args_kwargs () {
else else
# Save kwarg key and process next value. # Save kwarg key and process next value.
flags+=( ${arg#-} ) flags+=( ${arg#-} )
# Optionally populate global flags.
if [[ ${set_global_flags} == true ]]
then
global_flags+=( ${arg#-} )
fi
fi fi
else else
...@@ -418,9 +441,22 @@ handle_args_kwargs () { ...@@ -418,9 +441,22 @@ handle_args_kwargs () {
# Set key-value kwarg pair. # Set key-value kwarg pair.
kwargs[${kwarg_key}]=${arg} kwargs[${kwarg_key}]=${arg}
handle_kwarg=false handle_kwarg=false
# Optionally populate global kwargs.
if [[ ${set_global_kwargs} == true ]]
then
global_kwargs[${kwarg_key}]=${arg}
fi
else else
# Add arg to list of args. # Add arg to list of args.
args+=( ${arg} ) args+=( ${arg} )
# Optionally populate global args.
if [[ ${set_global_args} == true ]]
then
global_args+=( ${arg} )
fi
fi fi
fi fi
......
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