From 0258711bbc2a6da4ec1ce8da0500b4e0c604ec41 Mon Sep 17 00:00:00 2001 From: sergiojvg Date: Tue, 19 Mar 2019 05:25:56 -0600 Subject: [PATCH] F #2505: Add nested filters (#3083) --- src/cli/cli_helper.rb | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/cli/cli_helper.rb b/src/cli/cli_helper.rb index 213442fbdda..d5c7062cb64 100644 --- a/src/cli/cli_helper.rb +++ b/src/cli/cli_helper.rb @@ -55,6 +55,14 @@ module CLIHelper :description => "Filter data. An array is specified with\n"<< " "*31<<"column=value pairs." } + OPERATOR = { + :name => "operator", + :large => "--operator operator", + :format => String, + :description => "Logical operator used on filters: AND, OR."<< + " Default: AND." + } + # #HEADER = { # :name => "header", @@ -82,7 +90,7 @@ module CLIHelper } #OPTIONS = [LIST, ORDER, FILTER, HEADER, DELAY] - OPTIONS = [LIST, LISTCONF, DELAY, FILTER, CSV_OPT, NO_PAGER] + OPTIONS = [LIST, LISTCONF, DELAY, FILTER, OPERATOR, CSV_OPT, NO_PAGER] # Sets bold font def CLIHelper.scr_bold @@ -330,7 +338,7 @@ def data_array(data, options) } if options - filter_data!(res_data, options[:filter]) if options[:filter] + filter_data!(res_data, options) if options[:filter] sort_data!(res_data, options[:order]) if options[:order] end @@ -393,10 +401,16 @@ def header_str }.compact.join(' ') end - def filter_data!(data, filter) + def filter_data!(data, options) # TBD: add more operators # operators=/(==|=|!=|<|<=|>|>=)/ operators=/(=|!=)/ + filter = options[:filter] + if options.key?(:operator) + log_operator = options[:operator].upcase + else + log_operator = "AND" + end stems=filter.map do |s| m=s.match(/^(.*?)#{operators}(.*?)$/) @@ -427,12 +441,19 @@ def filter_data!(data, filter) stems.each do |s| if d[s[:index]].public_send(s[:operator] == "=" ? "==" : s[:operator], s[:right]) - pass=false - break + if log_operator == "OR" + pass = true + break + end + else + pass = false + if log_operator == "AND" + break + end end end - !pass + pass end end