Module: Threatinator::CLI::Helpers

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

Instance Method Summary collapse

Instance Method Details

#add_cli_args(gli, config_properties) ⇒ Object



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

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



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

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



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

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

#nest_opts(opts) ⇒ Object



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

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