Module: Antrapol::ToolRack::ArgUtils::ClassMethods

Includes:
ConditionUtils
Defined in:
lib/toolrack/arg_utils.rb

Instance Method Summary collapse

Instance Method Details

#aliasesObject



76
77
78
79
80
81
# File 'lib/toolrack/arg_utils.rb', line 76

def aliases
  if @_aliases.nil?
    @_aliases = {}
  end
  @_aliases
end

#arg_optionsObject



69
70
71
72
73
74
# File 'lib/toolrack/arg_utils.rb', line 69

def arg_options
  if @_arg_options.nil?
    @_arg_options = {}
  end
  @_arg_options
end

#arg_spec(&block) ⇒ Object



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

def arg_spec(&block)
  class_eval(&block) 
end

#callback(evt, opts = {}, &block) ⇒ Object



57
58
59
60
# File 'lib/toolrack/arg_utils.rb', line 57

def callback(evt, opts = {}, &block)
  raise ArgParsingException, "Event should not be nil" if evt.nil? 
  callbacks[evt] = { opts: opts, cb: block }
end

#callbacksObject



83
84
85
86
87
88
# File 'lib/toolrack/arg_utils.rb', line 83

def callbacks
  if @_cb.nil?
    @_cb = {}
  end
  @_cb
end

#is_all_required_satisfy?(arr) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/toolrack/arg_utils.rb', line 35

def is_all_required_satisfy?(arr)
  if arr.is_a?(Array)
    res = true
    required.each do |f|
      res = arr.include?(f) 
      if not res
        al = aliases.invert[f]
        res = arr.include?(al)
        raise RequiredFieldEmpty, "Required parameter '#{f}' is not given" if not res
      end
    end
    res

  else
    raise ArgParsingException, "Given array to check for required fields is not an array. Got #{arr}"
    if arr.is_a?(ArgParsingException)
      STDERR.puts "Error throwing into the method was : #{arr.message}"
    end

  end
end

#loggerObject



105
106
107
# File 'lib/toolrack/arg_utils.rb', line 105

def logger
  Antrapol::ToolRack.logger(:c_arg_utils)
end

#opt(key, desc, opts = {}, &block) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
# File 'lib/toolrack/arg_utils.rb', line 21

def opt(key, desc, opts = {}, &block)
  raise RequiredFieldEmpty, "Key field cannot be empty" if is_empty?(key)
  raise DuplicatedField, "Given key '#{key}' already exist" if arg_options.keys.include?(key)
  raise RequiredFieldEmpty, "Block is require" if not block

  arg_options[key] = { key: key, desc: desc, arg_options: opts, callback: block }
  required << key if opts[:required] == true
end

#opt_alias(existing, new) ⇒ Object

Raises:



30
31
32
33
# File 'lib/toolrack/arg_utils.rb', line 30

def opt_alias(existing, new)
  raise InvalidKey, "Existing key '#{existing}'to map to new key '#{new}' not found" if not arg_options.keys.include?(existing)
  aliases[new] = existing 
end

#requiredObject



62
63
64
65
66
67
# File 'lib/toolrack/arg_utils.rb', line 62

def required
  if @_req.nil?
    @_req = []
  end
  @_req
end

#set_value_separator(val) ⇒ Object

def value_separator=(val)

logger.debug "Setting value separator #{val}"
@_valSep = val

end



94
95
96
# File 'lib/toolrack/arg_utils.rb', line 94

def set_value_separator(val)
  @_valSep = val
end

#value_separatorObject



98
99
100
101
102
103
# File 'lib/toolrack/arg_utils.rb', line 98

def value_separator
  if @_valSep.nil?
    @_valSep = " "
  end
  @_valSep
end