Modify switch handling to centralize parse and validation for --{foo}-is-empty

Signed-off-by: Joey Armstrong <jarmstrong@linuxfoundation.org>
Change-Id: Ia8b955f5451b5b7938d48abdea681e5c8396b376
diff --git a/jira/bin/jira-search.sh b/jira/bin/jira-search.sh
index 751edb6..9015a38 100755
--- a/jira/bin/jira-search.sh
+++ b/jira/bin/jira-search.sh
@@ -539,10 +539,6 @@
 
 while [ $# -gt 0 ]; do
 
-#    if [ ${#suffix0[@]} -gt 0 ]; then
-#        suffix0+=('AND')
-#    fi
-
     arg="$1"; shift
     banner "ARG=[$arg], \$@=[$@]"
     
@@ -555,31 +551,21 @@
         '--help')    usage; exit 0                      ;;
         '--help-'*)  help_with "${arg/--help-/}"        ;;
         '--usage-'*) help_usage_show "${arg/--usage-/}" ;;
+ 
+        *'-is-empty'*)
+            is_switch_valid__is_empty "$arg"
 
-        '--'*'-is-empty')
             declare -a args=()
-            args+=('--is-empty')
+            arg="${arg:2}"               # remove prefix --
+            arg="${arg%-is-empty}"       # remove suffix token-name
 
-            arg="${arg:2}"           # remove prefix --
-            arg="${arg%-is-empty}"   # remove suffix token-name
-            args+=("$arg")
+            ## Rewrite switch to bypass is-empty and match --required
+            args+=("--${arg}+is-empty")  # ARGV: gen JCL tokens
             [[ $# -gt 0 ]] && { args+=("$@"); }
 
-            set -- "${args[@]}"
+            set -- "${args[@]}" # --is-empty resolved
             ;;
-        
-        '--is-empty')
-            declare val="$1"; shift
-            declare -a valid=()
-            valid+=('label')
 
-            if [[ " ${valid[@]} " =~ " ${val} " ]]; then
-                is_empty+=("$val")
-            else
-                error "Detected invalid --is-empty switch [$arg]"
-            fi
-        ;;
-        
         ##-----------------##
         ##---]  MODES  [---##
         ##-----------------##
@@ -703,7 +689,7 @@
             esac
             ;;
 
-        '--resolved-'*)
+        '--resolved'*) # [-+]
             # function get_jql_reasons()
             case "$arg" in
 
@@ -711,7 +697,10 @@
                 *end) declare -g resolved_end="$1";   shift ;;
 
                 *not-empty) declare -g resolved_not_empty="$1" ;;
-                *empty) declare -g resolved_is_empty="$1"  ;;
+                *'empty')
+                    banner "Detected [$arg]"
+                    declare -g resolved_is_empty="$1"
+                    ;;
 
                 *excl)
                     [[ ! -v resolved_excl ]] && { declare -g -a resolved_excl=(); }
@@ -823,16 +812,19 @@
 [[ -v components ]] && { do_components components suffix0; }
 do_labels labels_incl labels_excl suffix0
 do_text  text                     suffix0
-do_resolved                       suffix0
+
+get_jcl_resolved                  suffix0
+    # do_resolved                       suffix0
+
 do_fixversion                     suffix0
 
-declare -p suffix0
-
-if [[ -v getopt_argv_AND ]]; then
-    query="$(join_by 'AND' "${suffix0[0]}")"
-elif [[ -v getopt_argv_OR ]]; then
+declare -p getopt_argv_any_OR
+if [[ -v getopt_argv_any_OR ]]; then
     query="$(join_by 'OR' "${suffix0[0]}")"
+elif [[ -v getopt_argv_any_AND ]]; then
+    query="$(join_by 'AND' "${suffix0[0]}")"
 else
+#    query="$(join_by 'OR' "${suffix0[0]}")"
     error "Ambiguous query [argv needs: --and or --or]"
 fi
 
diff --git a/jira/jira-search/getopt/detect-modifiers.sh b/jira/jira-search/getopt/detect-modifiers.sh
index 84a1afd..22c1cae 100644
--- a/jira/jira-search/getopt/detect-modifiers.sh
+++ b/jira/jira-search/getopt/detect-modifiers.sh
@@ -87,7 +87,17 @@
         [[ -v getopt_argv_NOT ]] && { declare -p getopt_argv_NOT; }
         [[ -v getopt_argv_OR ]] && { declare -p getopt_argv_OR; }
     fi
-    
+
+    ## Persistent flags
+    [[ -v getopt_argv_AND ]] && { declare -g -i getopt_argv_any_AND=1; }
+    [[ -v getopt_argv_OR ]]  && { declare -g -i getopt_argv_any_OR=1; }
+
+    if false; then
+       [[ -v getopt_argv_any_AND ]] && { declare -g -i getopt_argv_any_AND; }
+       [[ -v getopt_argv_any_OR ]]  && { declare -g -i getopt_argv_any_OR; }
+    fi
+
+
     : # return $?==0
     return
 }
diff --git a/jira/jira-search/include.sh b/jira/jira-search/include.sh
index 57405c9..8f81512 100644
--- a/jira/jira-search/include.sh
+++ b/jira/jira-search/include.sh
@@ -44,6 +44,7 @@
 #source "$pgmlib/fixversion.sh"
 #source "$pgmlib/resolved.sh"
 source "${pgm_lib}/fixversion.sh"
+source "${pgm_lib}/is_empty.sh"
 source "${pgm_lib}/resolved.sh"
 source "${pgm_lib}/help/utils.sh"
 
diff --git a/jira/jira-search/is_empty.sh b/jira/jira-search/is_empty.sh
new file mode 100644
index 0000000..fe6e9d1
--- /dev/null
+++ b/jira/jira-search/is_empty.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+## -----------------------------------------------------------------------
+## -----------------------------------------------------------------------
+
+## -----------------------------------------------------------------------
+## Intent: Detect if the --{labels,required}-is-empty switch is valid
+## -----------------------------------------------------------------------
+function is_switch_valid__is_empty()
+{
+    local raw="$1"; shift # --{foo}-is-empty
+
+    declare -a valid=()
+    valid+=('label')
+    valid+=('resolved')
+
+    # --required-is-empty       => required
+    local val="$raw"
+    val="${val:2}"               # remove prefix --
+    val="${val%-is-empty}"       # remove suffix token-name
+
+    if [[ " ${valid[@]} " =~ " ${val} " ]]; then
+        is_empty+=("$val")
+    else
+        error "Detected invalid --is-empty switch [$arg]"
+    fi
+    
+    return
+}
+
+: # ($?=0) for source $include
+
+# [EOF]
diff --git a/jira/jira-search/resolved.sh b/jira/jira-search/resolved.sh
index 9d2d563..88b6835 100644
--- a/jira/jira-search/resolved.sh
+++ b/jira/jira-search/resolved.sh
@@ -1,4 +1,25 @@
 #!/bin/bash
+## -----------------------------------------------------------------------
+## -----------------------------------------------------------------------
+
+[[ ! -v JCL_resovled ]] && { declare -g -a JCL_resolved=(); }
+
+## --------------------------------------------------------------------
+## Intent: Retrieve a list of reason query strings
+## --------------------------------------------------------------------
+function get_jcl_resolved()
+{
+    local -n ref=$1; shift
+
+    local -a tmp=()
+    do_resolved tmp
+    ref+=("$tmp")
+    
+    local query="$(join_by 'OR' "${tmp[0]}")"
+    ref+=("($query)")
+    
+    return
+}
 
 ## --------------------------------------------------------------------
 ## Intent: Retrieve a list of reason query strings
@@ -22,7 +43,6 @@
 function do_resolved()
 {
     declare -n ans=$1; shift
-   # declare -g resolved
 
     [[ -v resolved_start ]] && { ans+=("(Resolved >= $resolved_start)"); }
     [[ -v resolved_end ]]   && { ans+=("(Resolved <= $resolved_end)"); }
@@ -38,7 +58,10 @@
         ans+=( "(resolution IN ($filter))" )
     fi
 
-    [[ -v resolved_not_empty ]] && { ans+=('(resolved IS NOT EMPTY)'); }
+    [[ -v resolved_not_empty ]] \
+        && { ans+=('(resolved IS NOT EMPTY)'); } \
+        || { true; }
+               
     [[ -v resolved_is_empty ]] \
         && { ans+=('(resolved IS EMPTY)'); } \
         || { true; }
diff --git a/jira/makefile b/jira/makefile
index ab4c3eb..3a6a316 100644
--- a/jira/makefile
+++ b/jira/makefile
@@ -6,7 +6,8 @@
 
 ## -----------------------------------------------------------------------
 ## -----------------------------------------------------------------------
-all:
+all: ../lf
+	cd .. && $(MAKE)
 
 ## -----------------------------------------------------------------------
 ## -----------------------------------------------------------------------
@@ -16,13 +17,18 @@
 ## -----------------------------------------------------------------------
 ## -----------------------------------------------------------------------
 query-args	:= $(null)
-# query-args	+= --dry-run
+ifdef DRYRUN
+  query-args	+= --dry-run
+endif
 query-args	+= --opencord
 query-args	+= --or
-query-args	+= --label-is-empty
-query-args	+= --label   'python'
-query-args	+= --label-excl 'foobar'
+# query-args	+= --label-is-empty
+query-args	+= --label   'python3x'
+#query-args	+= --label   'python'
+#query-args	+= --label-excl 'foobar'
 query-args	+= --text-or 'python'
+query-args	+= --resolved-is-empty
+# query-args	+= --unresolved
 
 query :
 	$(jira-search) $(query-args) 2>&1 | tee log