diff --git a/readme.md b/readme.md index a31ffce8ceee27fbad70db89cdea02f40bc6865c..08d59f423af5e8c07a09cb2e9184eb2e83f8fb07 100644 --- a/readme.md +++ b/readme.md @@ -46,12 +46,13 @@ These execute automatically on script import. * `handle_args_kwargs` - Parses all passed args into 3 sets: * `args` - Any values that do not match the other two below types. * Ex: `True`, `5`, and `test`. - * `flags` - Any values that start with "-". - * Interpreted as a boolean. True if present in `flags` array, False if not. - * Ex: `-a`, `-b`, and `-run`. - * `Kwargs` - Key-value pairs, where the key comes first and starts with "--". Associated value is + * `Kwargs` - Key-value pairs, where the key comes first and starts with "--" or "-". * Note that the associated value must come immediately after the key. - * Ex: `--type int`, `--dir ./test`, and `--name Bob`. + * Ex: `-type int`, `--dir ./test`, and `--name Bob`. + * `flags` - Any values that start with "-" or "--", and must be defined by the calling script. + * To define flags, populate a `possible_flags` array with the flag values prior to calling function or util script. + * Interpreted as a boolean. True if present in `flags` array, False if not. + * Ex: `-a`, `--b`, `-run`, and `--test`. ### Directory Functions * `get_absolute_path` - Gets absolute path of passed location. diff --git a/utils.sh b/utils.sh index 0f2162bc7f3dbc9b0aa2dbec55eb528c1c1becab..8e1fb54b348661c7644e449654100a0349c0747e 100755 --- a/utils.sh +++ b/utils.sh @@ -65,12 +65,14 @@ function normalize_terminal () { # Splits into "args", "kwargs", and "flags": # * Args are any other values that don't match below two formats. # * Ex: "True", "5", and "test". - # * 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 "--". + # * Kwargs are key-value pairs, where the key comes first and starts with "--" or "-". # * Note that the associated value must come immediately after the key. # * Ex: "--type int", "--dir ./test", and "--name Bob". + # * Flags are any variable that starts with "-" or "--", and must be defined by the calling script. + # * To define flags, populate a "possible_flags" array with flag values prior to calling util script. + # * These are effectively treated as booleans. True if present, false otherwise. + # * Ex: "-a", "--b", "-run", and "--test". + # # Ordering of provided args, kwargs, and flags should not matter, aside from kwarg keys needing a corresponding value # as the direct next processed value. @@ -111,38 +113,65 @@ handle_args_kwargs () { # Special handling for help flags. help_flags=true - # Handle for kwargs. - elif [[ "${arg}" == "--"* && "${arg}" != "---"* ]] + # Handle for flags. + elif [[ ${#possible_flags[@]} > 0 && ${possible_flags[@]} =~ "${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}" + # 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. - kwarg_key=${arg#--} - handle_kwarg=true + if [[ "${arg}" == "--"* ]] + then + # Handle for double dash flag. + local new_flag=${arg#--} + elif [[ "${arg}" == "-"* ]] + then + # Handle for single dash flag. + local new_flag=${arg#-} + else + # Unexpected kwarg value. + echo -e "${text_red}Unexpected flag type of \"${arg}\". Unable to proceed.${text_reset}" + exit 1 + fi + + flags+=( ${new_flag} ) + + # Optionally populate global flags. + if [[ ${set_global_flags} == true ]] + then + global_flags+=( ${new_flag} ) + fi fi - # Handle for flags. - elif [[ "${arg}" == "-"* && "${arg}" != "--"* ]] + # Handle for kwargs. + elif [[ ("${arg}" == "-"* || "${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}" + # 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. - flags+=( ${arg#-} ) - - # Optionally populate global flags. - if [[ ${set_global_flags} == true ]] + if [[ "${arg}" == "--"* ]] + then + # Handle for two dash kwarg. + kwarg_key=${arg#--} + handle_kwarg=true + elif [[ "${arg}" == "-"* ]] then - global_flags+=( ${arg#-} ) + # Handle for one dash kwarg. + kwarg_key=${arg#-} + handle_kwarg=true + else + # Unexpected kwarg value. + echo -e "${text_red}Unexpected kwarg key type of \"${arg}\". Unable to proceed.${text_reset}" + exit 1 fi fi