Class: CommandLineArgsToHash

Inherits:
Object
  • Object
show all
Defined in:
lib/scheduler_daemon/command_line_args_to_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options = {}) ⇒ CommandLineArgsToHash

Returns a new instance of CommandLineArgsToHash.



17
18
19
20
# File 'lib/scheduler_daemon/command_line_args_to_hash.rb', line 17

def initialize(args, options = {})
  @args = args
  @options = options
end

Class Method Details

.parse(args, options = {}) ⇒ Object



13
14
15
# File 'lib/scheduler_daemon/command_line_args_to_hash.rb', line 13

def self.parse(args, options = {})
  new(args, options).parse
end

Instance Method Details

#format_value(k, v) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/scheduler_daemon/command_line_args_to_hash.rb', line 39

def format_value(k, v)
  if @options[:array_args] && @options[:array_args].include?(k)
    v = v.split(/,\s*/) if v.respond_to?(:split)
  end
  v = false if v == 'false'
  v = true if v == 'true'
  v
end

#parseObject



22
23
24
25
26
27
28
29
# File 'lib/scheduler_daemon/command_line_args_to_hash.rb', line 22

def parse
  hash = ::HashWithIndifferentAccess.new
  @args.each{|arg|
    k, v = read_argument(arg)
    hash[k] = v
  }
  hash
end

#read_argument(arg) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/scheduler_daemon/command_line_args_to_hash.rb', line 31

def read_argument(arg)
  k, v = arg.sub(/^\-\-/, '').split('=')
  k = k.gsub(/-/, '_') # replace - to _ so that --skip-init becomes options[:skip_init]
  v = true if v.nil? # default passed in args to true.
  v = format_value(k, v)
  [k, v]
end