<commit_before>################################################################################
#
# searchoptions.rb
#
# class SearchOptions: defines the available command-line options and
#                      corresponding utility methods
#
################################################################################

require 'rexml/document'
include REXML
require_relative 'config.rb'
require_relative 'searchoption.rb'
require_relative 'searchsettings.rb'

class SearchOptions
  
  def initialize
    @options = []
    @arg_dict = {}
    @flag_dict = {}
    set_actions
    set_options_from_xml
    @options.sort! { |a, b| a.sortarg <=> b.sortarg }
  end

  def set_actions
    @arg_action_dict = {
      'in-archiveext' => ->(x, settings){ settings.add_comma_delimited_exts(x,
          settings.in_archiveextensions) },
      'in-archivefilepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.in_archivefilepatterns) },
      'in-dirpattern' => ->(x, settings){ settings.add_pattern(x,
        settings.in_dirpatterns) },
      'in-ext' => ->(x, settings){ settings.add_comma_delimited_exts(x,
          settings.in_extensions) },
      'in-filepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.in_filepatterns) },
      'in-linesafterpattern' => ->(x, settings){ settings.add_pattern(x,
        settings.in_linesafterpatterns) },
      'in-linesbeforepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.in_linesbeforepatterns) },
      'linesafter' => ->(x, settings){ settings.linesafter = x.to_i },
      'linesaftertopattern' => ->(x, settings){ settings.add_pattern(x,
        settings.linesaftertopatterns) },
      'linesafteruntilpattern' => ->(x, settings){ settings.add_pattern(x,
        settings.linesafteruntilpatterns) },
      'linesbefore' => ->(x, settings){ settings.linesbefore = x.to_i },
      'maxlinelength' => ->(x, settings){ settings.maxlinelength = x.to_i },
      'out-archiveext' => ->(x, settings){ settings.add_comma_delimited_exts(x,
          settings.out_archiveextensions) },
      'out-archivefilepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.out_archivefilepatterns) },
      'out-dirpattern' => ->(x, settings){ settings.add_pattern(x,
        settings.out_dirpatterns) },
      'out-ext' => ->(x, settings){ settings.add_comma_delimited_exts(x,
          settings.out_extensions) },
      'out-filepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.out_filepatterns) },
      'out-linesafterpattern' => ->(x, settings){ settings.add_pattern(x,
        settings.out_linesafterpatterns) },
      'out-linesbeforepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.out_linesbeforepatterns) },
      'search' => ->(x, settings){ settings.add_pattern(x,
        settings.searchpatterns) }
    }
    @flag_action_dict = {
      'allmatches' => ->(settings){ settings.firstmatch = false },
      'archivesonly' => ->(settings){ settings.archivesonly = true;
        settings.searcharchives = true },
      'caseinsensitive' => ->(settings){ settings.casesensitive = false },
      'casesensitive' => ->(settings){ settings.casesensitive = true },
      'debug' => ->(settings){ settings.debug = true; settings.verbose = true },
      'dotiming' => ->(settings){ settings.dotiming = true },
      'excludehidden' => ->(settings){ settings.excludehidden = true },
      'firstmatch' => ->(settings){ settings.firstmatch = true },
      'help' => ->(settings){ settings.printusage = true },
      'includehidden' => ->(settings){ settings.excludehidden = false },
      'listdirs' => ->(settings){ settings.listdirs = true },
      'listfiles' => ->(settings){ settings.listfiles = true },
      'listlines' => ->(settings){ settings.listlines = true },
      'multilinesearch' => ->(settings){ settings.multilinesearch = true },
      'noprintmatches' => ->(settings){ settings.printresults = false },
      'norecursive' => ->(settings){ settings.recursive = false },
      'nosearcharchives' => ->(settings){ settings.searcharchives = false },
      'printmatches' => ->(settings){ settings.printresults = true },
      'recursive' => ->(settings){ settings.recursive = true },
      'searcharchives' => ->(settings){ settings.searcharchives = true },
      'uniquelines' => ->(settings){ settings.uniquelines = true },
      'verbose' => ->(settings){ settings.verbose = true },
      'version' => ->(settings){ settings.printversion = true }
    }
  end

  def set_options_from_xml
    doc = Document.new(File.new(File.expand_path(SEARCHOPTIONSPATH)))
    doc.elements.each('searchoptions/searchoption') { |searchoption|
      long = searchoption.attributes['long']
      short = searchoption.attributes['short']
      desc = searchoption.text.strip
      func = nil
      if @arg_action_dict.has_key?(long)
        func = @arg_action_dict[long]
      elsif @flag_action_dict.has_key?(long)
        func = @flag_action_dict[long]
      else
        raise ArgumentError, "Unknown search option: #{long}"
      end
      option = SearchOption.new(short, long, desc, func)
      @options.push(option)
      if @arg_action_dict.has_key?(long)
        @arg_dict[long] = option
        if short
          @arg_dict[short] = option
        end
      elsif @flag_action_dict.has_key?(long)
        @flag_dict[long] = option
        if short
          @flag_dict[short] = option
        end
      end
    }
  end

  def search_settings_from_args(args)
    settings = SearchSettings.new()
    settings.printresults = true
    while args.count > 0
      arg = args.shift
      if arg.start_with?('-')
        while arg and arg.start_with?('-')
          arg = arg[1..arg.length]
        end
        if @arg_dict.has_key?(arg)
          if args
            argval = args.shift<commit_msg>Changed dict keys to symbols in ruby<commit_after>################################################################################
#
# searchoptions.rb
#
# class SearchOptions: defines the available command-line options and
#                      corresponding utility methods
#
################################################################################

require 'rexml/document'
include REXML
require_relative 'config.rb'
require_relative 'searchoption.rb'
require_relative 'searchsettings.rb'

class SearchOptions
  
  def initialize
    @options = []
    @arg_dict = {}
    @flag_dict = {}
    set_actions
    set_options_from_xml
    @options.sort! { |a, b| a.sortarg <=> b.sortarg }
  end

  def set_actions
    @arg_action_dict = {
      :'in-archiveext' => ->(x, settings){ settings.add_comma_delimited_exts(x,
          settings.in_archiveextensions) },
      :'in-archivefilepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.in_archivefilepatterns) },
      :'in-dirpattern' => ->(x, settings){ settings.add_pattern(x,
        settings.in_dirpatterns) },
      :'in-ext' => ->(x, settings){ settings.add_comma_delimited_exts(x,
          settings.in_extensions) },
      :'in-filepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.in_filepatterns) },
      :'in-linesafterpattern' => ->(x, settings){ settings.add_pattern(x,
        settings.in_linesafterpatterns) },
      :'in-linesbeforepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.in_linesbeforepatterns) },
      :'linesafter' => ->(x, settings){ settings.linesafter = x.to_i },
      :'linesaftertopattern' => ->(x, settings){ settings.add_pattern(x,
        settings.linesaftertopatterns) },
      :'linesafteruntilpattern' => ->(x, settings){ settings.add_pattern(x,
        settings.linesafteruntilpatterns) },
      :'linesbefore' => ->(x, settings){ settings.linesbefore = x.to_i },
      :'maxlinelength' => ->(x, settings){ settings.maxlinelength = x.to_i },
      :'out-archiveext' => ->(x, settings){ settings.add_comma_delimited_exts(x,
          settings.out_archiveextensions) },
      :'out-archivefilepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.out_archivefilepatterns) },
      :'out-dirpattern' => ->(x, settings){ settings.add_pattern(x,
        settings.out_dirpatterns) },
      :'out-ext' => ->(x, settings){ settings.add_comma_delimited_exts(x,
          settings.out_extensions) },
      :'out-filepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.out_filepatterns) },
      :'out-linesafterpattern' => ->(x, settings){ settings.add_pattern(x,
        settings.out_linesafterpatterns) },
      :'out-linesbeforepattern' => ->(x, settings){ settings.add_pattern(x,
        settings.out_linesbeforepatterns) },
      :'search' => ->(x, settings){ settings.add_pattern(x,
        settings.searchpatterns) }
    }
    @flag_action_dict = {
      :'allmatches' => ->(settings){ settings.firstmatch = false },
      :'archivesonly' => ->(settings){ settings.archivesonly = true;
        settings.searcharchives = true },
      :'caseinsensitive' => ->(settings){ settings.casesensitive = false },
      :'casesensitive' => ->(settings){ settings.casesensitive = true },
      :'debug' => ->(settings){ settings.debug = true; settings.verbose = true },
      :'dotiming' => ->(settings){ settings.dotiming = true },
      :'excludehidden' => ->(settings){ settings.excludehidden = true },
      :'firstmatch' => ->(settings){ settings.firstmatch = true },
      :'help' => ->(settings){ settings.printusage = true },
      :'includehidden' => ->(settings){ settings.excludehidden = false },
      :'listdirs' => ->(settings){ settings.listdirs = true },
      :'listfiles' => ->(settings){ settings.listfiles = true },
      :'listlines' => ->(settings){ settings.listlines = true },
      :'multilinesearch' => ->(settings){ settings.multilinesearch = true },
      :'noprintmatches' => ->(settings){ settings.printresults = false },
      :'norecursive' => ->(settings){ settings.recursive = false },
      :'nosearcharchives' => ->(settings){ settings.searcharchives = false },
      :'printmatches' => ->(settings){ settings.printresults = true },
      :'recursive' => ->(settings){ settings.recursive = true },
      :'searcharchives' => ->(settings){ settings.searcharchives = true },
      :'uniquelines' => ->(settings){ settings.uniquelines = true },
      :'verbose' => ->(settings){ settings.verbose = true },
      :'version' => ->(settings){ settings.printversion = true }
    }
  end

  def set_options_from_xml
    doc = Document.new(File.new(File.expand_path(SEARCHOPTIONSPATH)))
    doc.elements.each('searchoptions/searchoption') { |searchoption|
      long = searchoption.attributes['long']
      short = searchoption.attributes['short']
      desc = searchoption.text.strip
      func = nil
      if @arg_action_dict.has_key?(long.to_sym)
        func = @arg_action_dict[long.to_sym]
      elsif @flag_action_dict.has_key?(long.to_sym)
        func = @flag_action_dict[long.to_sym]
      else
        raise ArgumentError, "Unknown search option: #{long}"
      end
      option = SearchOption.new(short, long, desc, func)
      @options.push(option)
      if @arg_action_dict.has_key?(long.to_sym)
        @arg_dict[long] = option
        if short
          @arg_dict[short] = option
        end
      elsif @flag_action_dict.has_key?(long.to_sym)
        @flag_dict[long] = option
        if short
          @flag_dict[short] = option
        end
      end
    }
  end

  def search_settings_from_args(args)
    settings = SearchSettings.new()
    settings.printresults = true
    while args.count > 0
      arg = args.shift
      if arg.start_with?('-')
        while arg and arg.start_with?('-')
          arg = arg[1..arg.length]
        end
        if @arg_dict.has_key?(arg)
          if args
            argval = args.shift
{'fraction_non_alphanumeric': 0.17486848803665367, 'fraction_numerical': 0.00033938571186153065, 'mean_word_length': 3.2171735241502684, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 179, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2465390', 'n_tokens_mistral': 3587, 'n_tokens_neox': 3513, 'n_words': 647}