Module: Threatinator::CLI::Helpers

Defined in:
lib/threatinator/cli/parser.rb

Instance Method Summary collapse

Instance Method Details

#add_cli_args(gli, config_properties) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/threatinator/cli/parser.rb', line 12

def add_cli_args(gli, config_properties)
  config_properties.each do |key, args|
    desc, type, default_value = args
    if type.base == Axiom::Types::Boolean
      gli.switch key, desc: desc
    elsif type.base ==Axiom::Types::Array
      gli.flag key, desc: desc, type: Array
    else 
      gli.flag key, desc: desc
    end
  end
end

#clean_opts(opts) ⇒ Object



25
26
27
28
# File 'lib/threatinator/cli/parser.rb', line 25

def clean_opts(opts)
  opts.delete_if {|k, v| k.kind_of?(::Symbol) or (k == 'help') or v.nil? }
  opts
end

#fix_opts(opts) ⇒ Object



46
47
48
# File 'lib/threatinator/cli/parser.rb', line 46

def fix_opts(opts)
  nest_opts(clean_opts(opts))
end

#nest_opts(opts) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/threatinator/cli/parser.rb', line 30

def nest_opts(opts)
  config_hash = {}
  opts.each_pair do |key, val|
    key_path = key.to_s.split('.')
    final_key = key_path.pop
    nested_hash = config_hash
    while key_path.length > 0
      part = key_path.shift
      nested_hash[part] ||= {}
      nested_hash = nested_hash[part]
    end
    nested_hash[final_key] = val
  end
  config_hash
end