Joey Armstrong | f8c78f0 | 2024-08-28 17:00:29 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | ## ----------------------------------------------------------------------- |
| 3 | ## Intent: getopts parsing and set flag status for --is-empty swtiches |
| 4 | ## ----------------------------------------------------------------------- |
| 5 | |
| 6 | ## ----------------------------------------------------------------------- |
| 7 | ## Intent: Parse command line switch, when *-empty detected take action |
| 8 | ## - remove is-empty OR is-not-empty from swtich string |
| 9 | ## - set values in map %attrs=() to capture detection bits |
| 10 | ## Return: |
| 11 | ## %attrs |
| 12 | ## --is-empty empty=true |
| 13 | ## --is-not-empty empty=true, not=true |
| 14 | ## ----------------------------------------------------------------------- |
| 15 | function getopts_switch__empty() |
| 16 | { |
| 17 | local -n gse_arg=$1; shift |
| 18 | declare -g -A attrs |
| 19 | |
| 20 | case "$gse_arg" in |
| 21 | *'-is-empty') |
| 22 | is_switch_valid__is_empty "$gse_arg" |
| 23 | attrs['empty']=1 |
| 24 | # declare -g -i argv_is_empty=1 |
| 25 | gse_arg="${gse_arg/-is-empty/}" |
| 26 | local -i rc=0 |
| 27 | ;; |
| 28 | *) local -i rc=1 ;; |
| 29 | esac |
| 30 | |
| 31 | [[ $rc -eq 0 ]] && { true; } || { false; } |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | ## ----------------------------------------------------------------------- |
| 36 | ## Intent: Define flags based on detected switch modifiers |
| 37 | ## ----------------------------------------------------------------------- |
| 38 | function gen_attrs__empty() |
| 39 | { |
| 40 | local switch="$1"; shift |
| 41 | declare -g attrs |
| 42 | |
| 43 | local is="${switch}_is_empty" |
| 44 | local not="${switch}_not_empty" |
| 45 | declare -g -i "$is" |
| 46 | declare -g -i "$not" |
| 47 | |
| 48 | ## Clear prior state |
| 49 | [[ -v "$is" ]] && { echo "IS"; unset "$is"; } |
| 50 | [[ -v "$not" ]] && { echo "NOT"; unset "$not"; } |
| 51 | |
| 52 | ## Apply current attributes |
| 53 | ## [TODO] parameterize flag creation |
| 54 | if [[ ! -v attrs['empty'] ]]; then |
| 55 | : # fall through |
| 56 | elif [[ -v attrs['not'] ]]; then |
| 57 | declare -g -i $not=1 |
| 58 | else |
| 59 | declare -g -i $is=1 |
| 60 | fi |
| 61 | |
| 62 | : # assign ($?==0) |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | |
| 67 | # [EOF] |