Class: SimpleConsole::ParamsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/init.rb,
lib/params_parser.rb,
lib/simpleconsole.rb

Instance Method Summary collapse

Constructor Details

#initializeParamsParser

Returns a new instance of ParamsParser.



2
3
4
5
6
7
# File 'lib/params_parser.rb', line 2

def initialize
  @letter_to_key ||= Hash.new
  @key_types ||= Hash.new
  @letter_types ||= Hash.new
  @error_list ||= Array.new
end

Instance Method Details

#argv_to_params(argv) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/params_parser.rb', line 25

def argv_to_params(argv)
  params = Hash.new
  @letter_to_key ||= Hash.new
  argv.each do |arg|
    if arg =~ /^--(.*)$/
      argument = $1
      value = argv[argv.index(arg) + 1]
      if @letter_to_key.has_value?(argument.to_sym)
        params[argument.to_sym] = get_value_of(value, argument.to_sym)
      else
        add_error(arg)
      end
    elsif arg =~ /^-(.)/
      argument = $1
      value = argv[argv.index(arg) + 1]
      if @letter_to_key.has_key?(argument.to_sym)
        argument = @letter_to_key[argument.to_sym]
        params[argument.to_sym] = get_value_of(value, argument.to_sym)
      else
        add_error(arg)
      end
    end
  end

  return params
end

#bool_params(list) ⇒ Object



21
22
23
# File 'lib/params_parser.rb', line 21

def bool_params(list)
  set_key(list, :bool)
end

#int_params(list) ⇒ Object



9
10
11
# File 'lib/params_parser.rb', line 9

def int_params(list) 
  set_key(list, :int) 
end

#invalid_paramsObject



52
53
54
55
# File 'lib/params_parser.rb', line 52

def invalid_params
  @error_list ||= Array.new
  return @error_list
end

#string_params(list) ⇒ Object



17
18
19
# File 'lib/params_parser.rb', line 17

def string_params(list)
  set_key(list, :string)
end

#text_params(list) ⇒ Object



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

def text_params(list)
  set_key(list, :text)
end