Class: Optitron::Option::Opt

Inherits:
Optitron::Option show all
Defined in:
lib/optitron/option.rb

Constant Summary

Constants inherited from Optitron::Option

BOOLEAN_VALUES, FALSE_BOOLEAN_VALUES, TRUE_BOOLEAN_VALUES

Instance Attribute Summary collapse

Attributes inherited from Optitron::Option

#default, #desc, #has_default, #name, #parameterize, #required, #type

Instance Method Summary collapse

Methods inherited from Optitron::Option

#inclusion_test=, #interpolate_type, #validate

Constructor Details

#initialize(name, desc = nil, opts = nil) ⇒ Opt

Returns a new instance of Opt.



65
66
67
68
69
70
71
72
73
# File 'lib/optitron/option.rb', line 65

def initialize(name, desc = nil, opts = nil)
  if desc.is_a?(Hash)
    desc, opts = nil, desc
  end
  @name, @desc = name, desc
  @type = opts && opts[:type] || :boolean
  self.inclusion_test = opts[:in] if opts && opts[:in]
  self.default = opts && opts.key?(:default) ? opts[:default] : (@type == :boolean ? false : nil)
end

Instance Attribute Details

#short_nameObject

Returns the value of attribute short_name.



64
65
66
# File 'lib/optitron/option.rb', line 64

def short_name
  @short_name
end

Instance Method Details

#consume(response, tokens) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/optitron/option.rb', line 79

def consume(response, tokens)
  if opt_tok = tokens.find{|t| t.respond_to?(:name) and (t.name == short_name or t.name == name)}
    opt_tok_index = tokens.index(opt_tok)
    tokens.delete_at(opt_tok_index)
    case @type
    when :boolean
      value = if opt_tok.respond_to?(:value)
        opt_tok.value
      elsif opt_tok.name == short_name and tokens[opt_tok_index].respond_to?(:val) and BOOLEAN_VALUES.include?(tokens[opt_tok_index].val)
        tokens.delete_at(opt_tok_index).val
      end
      response.params_array << [self, value.nil? ? !default : value]
    when :numeric, :string
      value = if opt_tok.name == name
        if opt_tok.respond_to?(:value)
          opt_tok.value
        else
          response.add_error("missing", opt_tok.name)
        end
      elsif tokens[opt_tok_index].respond_to?(:val)
        tokens.delete_at(opt_tok_index).val
      elsif default
        default
      else
        response.add_error("required", opt_tok.name)
      end
      response.params_array << [self, value]
    when :array
      values = []
      values << opt_tok.value if opt_tok.respond_to?(:value)
      while tokens[opt_tok_index].respond_to?(:val)
        values << tokens.delete_at(opt_tok_index).val
      end
      response.params_array << [self, values]
    when :hash
      values = []
      if opt_tok.respond_to?(:value)
        response.add_error("not in the form key:value", name) if opt_tok.value[':'].nil?
        values << opt_tok.value.split(':', 2)
      end
      while tokens[opt_tok_index].respond_to?(:val) and !tokens[opt_tok_index].val[':'].nil?
        values << tokens.delete_at(opt_tok_index).val.split(':', 2)
      end
      response.params_array << [self, Hash[values]]
    else
      raise "unknown type: #{@type.inspect}"
    end
  end
end

#match?(tok) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/optitron/option.rb', line 75

def match?(tok)
  tok.respond_to?(:name) and [name, short_name].include?(tok.name)
end