From ec598d0fa57479f799736f447ae521aa49398b4e Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Sat, 10 Oct 2020 15:37:32 -0400 Subject: [PATCH] Add function to parse passed args, kwargs, and flags --- documents/references.md | 7 +++- utils.sh | 80 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/documents/references.md b/documents/references.md index fcb0919..bcd8f23 100644 --- a/documents/references.md +++ b/documents/references.md @@ -20,7 +20,9 @@ Various references used in project. <https://www.linuxjournal.com/content/return-values-bash-functions> ### Arrays -<https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays> +* General Arrays - <https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays> +* Get Array Length - <https://unix.stackexchange.com/a/193042> +* Check for Element - <https://stackoverflow.com/a/15394738> ### Dictionaries <https://stackoverflow.com/questions/1494178/how-to-define-hash-tables-in-bash> @@ -49,6 +51,9 @@ Various references used in project. #### String Trimming Manipulation <https://stackoverflow.com/a/14703709> +#### Substring Matching +<https://linuxize.com/post/how-to-check-if-string-contains-substring-in-bash/> + ### Other #### Check Number of Passed Args <https://stackoverflow.com/a/6482403> diff --git a/utils.sh b/utils.sh index ecb04b9..c833804 100755 --- a/utils.sh +++ b/utils.sh @@ -9,7 +9,7 @@ #region Global Utility Variables. -# Color Output. +# Color Output Variables. text_reset="\033[0m" text_black="\033[0;30m" text_red="\033[0;31m" @@ -21,6 +21,11 @@ text_cyan="\033[1;36m" text_yellow="\033[1;33m" text_white="\033[1;37m" +# Arg and Kwarg Holder Variables. +args="" +kwargs="" +flags="" + # Function Return Variables. return_value="" file_name="" @@ -351,6 +356,78 @@ function to_lower () { #endregion Text Manipulation 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. + ## +handle_args_kwargs () { + args=() + flags=() + declare -A kwargs + local handle_kwarg=false + local kwarg_key="" + + # Parse all args. + for arg in ${@} + do + # Check arg type, based on substring match. + if [[ "${arg}" == "--"* && "${arg}" != "---"* ]] + then + # Handle for kwargs. + + # 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 + + elif [[ "${arg}" == "-"* && "${arg}" != "--"* ]] + then + # Handle for flags. + + # 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#-} ) + fi + + else + # Handle for args. + + # Check for kwarg handling bool. + if [[ ${handle_kwarg} == true ]] + then + # Set key-value kwarg pair. + kwargs[${kwarg_key}]=${arg} + handle_kwarg=false + else + # Add arg to list of args. + args+=( ${arg} ) + fi + fi + + done +} + + ### # Prints out all available text colors provided by this script. ## @@ -372,3 +449,4 @@ function display_text_colors () { # Functions to call on script import. normalize_terminal +handle_args_kwargs ${@} -- GitLab