<commit_before>require 'thor'
require 'jerakia'
require 'json'
require 'yaml'

class Jerakia
  class CLI < Thor
    desc 'lookup [KEY]', 'Lookup [KEY] with Jerakia'
    option :config,
           aliases: :c,
           type: :string,
           desc: 'Configuration file'
    option :policy,
           aliases: :p,
           type: :string,
           default: 'default',
           desc: 'Lookup policy'
    option :namespace,
           aliases: :n,
           type: :string,
           default: '',
           desc: 'Lookup namespace'
    option :type,
           aliases: :t,
           type: :string,
           default: 'first',
           desc: 'Lookup type'
    option :scope,
           aliases: :s,
           type: :string,
           desc: 'Scope handler',
           default: "metadata"
    option :scope_options,
           type: :hash,
           desc: "Key/value pairs to be passed to the scope handler"
    option :merge_type,
           aliases: :m,
           type: :string,
           default: 'array',
           desc: 'Merge type'
    option :log_level,
           aliases: :l,
           type: :string,
           desc: 'Log level'
    option :verbose,
           aliases: :v,
           type: :boolean,
           desc: "Print verbose information"
    option :debug,
           aliases: :D,
           type: :boolean,
           desc: 'Debug information to console, implies --log-level debug'
    option :trace,
           type: :boolean,
           desc: 'Output stacktrace to stdout'
    option :metadata,
           aliases: :d,
           type: :hash,
           desc: 'Key/value pairs to be used as metadata for the lookup'
    option :schema,
           aliases: :S,
           type: :boolean,
           desc: 'Enable/disable schema lookup, default true',
           default: true
    option :output,
           aliases: :o,
           type: :string,
           default: 'json',
           desc: 'Output format, yaml or json'


    def lookup(key)

      case true
      when options[:verbose]
        loglevel = "verbose"
        logfile  = STDOUT
      when options[:debug]
        loglevel = "debug"
        logfile  = STDOUT
      else
        logfile = nil
        loglevel = options[:log_level]
      end

      begin

        jac = Jerakia.new({
          :config   => options[:config],
          :logfile  => logfile,
          :loglevel => loglevel,
          :trace    => options[:trace],
        })
        req = Jerakia::Request.new(
          :key           => key,
          :namespace     => options[:namespace].split(/::/),
          :policy        => options[:policy].to_sym,
          :lookup_type   => options[:type].to_sym,
          :merge         => options[:merge_type].to_sym,
          :metadata      => options[:metadata] || {},
          :scope         => options[:scope].to_sym,
          :scope_options => options[:scope_options],
          :use_schema    => options[:schema],
        )
<commit_msg>clone the cli options so they are not frozen<commit_after>require 'thor'
require 'jerakia'
require 'json'
require 'yaml'

class Jerakia
  class CLI < Thor
    desc 'lookup [KEY]', 'Lookup [KEY] with Jerakia'
    option :config,
           aliases: :c,
           type: :string,
           desc: 'Configuration file'
    option :policy,
           aliases: :p,
           type: :string,
           default: 'default',
           desc: 'Lookup policy'
    option :namespace,
           aliases: :n,
           type: :string,
           default: '',
           desc: 'Lookup namespace'
    option :type,
           aliases: :t,
           type: :string,
           default: 'first',
           desc: 'Lookup type'
    option :scope,
           aliases: :s,
           type: :string,
           desc: 'Scope handler',
           default: "metadata"
    option :scope_options,
           type: :hash,
           desc: "Key/value pairs to be passed to the scope handler"
    option :merge_type,
           aliases: :m,
           type: :string,
           default: 'array',
           desc: 'Merge type'
    option :log_level,
           aliases: :l,
           type: :string,
           desc: 'Log level'
    option :verbose,
           aliases: :v,
           type: :boolean,
           desc: "Print verbose information"
    option :debug,
           aliases: :D,
           type: :boolean,
           desc: 'Debug information to console, implies --log-level debug'
    option :trace,
           type: :boolean,
           desc: 'Output stacktrace to stdout'
    option :metadata,
           aliases: :d,
           type: :hash,
           desc: 'Key/value pairs to be used as metadata for the lookup'
    option :schema,
           aliases: :S,
           type: :boolean,
           desc: 'Enable/disable schema lookup, default true',
           default: true
    option :output,
           aliases: :o,
           type: :string,
           default: 'json',
           desc: 'Output format, yaml or json'


    def lookup(key)
      # Thor by default now returns a frozen options hash so we 
      # need to dup this here to prevent problems later with
      # modifying the request object
      #
      options_copy = options.dup

      case true
      when options[:verbose]
        loglevel = "verbose"
        logfile  = STDOUT
      when options[:debug]
        loglevel = "debug"
        logfile  = STDOUT
      else
        logfile = nil
        loglevel = options[:log_level]
      end

      begin

        jac = Jerakia.new({
          :config   => options[:config],
          :logfile  => logfile,
          :loglevel => loglevel,
          :trace    => options[:trace],
        })
        req = Jerakia::Request.new(
          :key           => key.dup,
          :namespace     => options_copy[:namespace].split(/::/),
          :policy        => options_copy[:policy].to_sym,
          :lookup_type   => options_copy[:type].to_sym,
          :merge         => options_copy[:merge_type].to_sym,
          :metadata      => options_copy[:metadata] || {},
          :scope         => options_copy[:scope].to_sym,
          :scope_options => options_copy[:scope_options],
          :use_schema    => options_copy[:schema],
        )

{'fraction_non_alphanumeric': 0.10885341074020319, 'fraction_numerical': 0.0, 'mean_word_length': 1.5713101160862355, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1024362', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1697, 'n_words': 509}