Class: UnimatrixParser::Option

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

Overview


Option Class

Constant Summary collapse

FLAG_TYPES =
[ :flag, :bool, :boolean ]
SINGLE_ARG_TYPES =
[ :int, :integer, :string, :double, :float, :io, :date ]
MULTI_ARG_TYPES =
[ :ints, :integers, :strings, :doubles, :floats, :ios, :dates ]
TYPES =
FLAG_TYPES + SINGLE_ARG_TYPES + MULTI_ARG_TYPES
INVALID_SHORT_ARG_REGEX =
/[\d-]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, options) ⇒ Option

Returns a new instance of Option.



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
# File 'lib/unimatrix_parser.rb', line 90

def initialize( name, description, options )
  options[ :description ] = description
  options[ :type ] = normalize_type( options[ :type ] )

  type_from_default = disambiguate_and_set_type_from_default( options )

  if options[ :type ] && type_from_default && options[ :type ] != type_from_default
    raise ArgumentError, ":type specification and default type don't match " +
      "(default type is #{ type_from_default })"
  end

  options[ :type ] = options[ :type ] || type_from_default || :flag
  options[ :long ] = set_long( options[ :long ], name )
  options[ :short ] = set_short( options[ :short ] )

  if options[ :short ] && options[ :short ] =~ INVALID_SHORT_ARG_REGEX
    raise ArgumentError, "a short option name can't be a number or a dash"
  end

  options[ :default ] = false if options[ :type ] == :flag && options[ :default ].nil?

  if options[ :default ] && options[ :multi ] && !options[ :default ].kind_of?( Array )
    options[ :default ] = [ options[ :default ] ] 
  end

  options[ :multi ] ||= false

  self.name = name
  self.options = options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



88
89
90
# File 'lib/unimatrix_parser.rb', line 88

def name
  @name
end

#optionsObject

Returns the value of attribute options.



88
89
90
# File 'lib/unimatrix_parser.rb', line 88

def options
  @options
end

Instance Method Details

#array_default?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/unimatrix_parser.rb', line 154

def array_default?
  options[ :default ].kind_of?( Array )
end

#defaultObject



150
151
152
# File 'lib/unimatrix_parser.rb', line 150

def default
  options[ :default ]
end

#descriptionObject



174
175
176
# File 'lib/unimatrix_parser.rb', line 174

def description
  options[ :description ]
end

#flag?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/unimatrix_parser.rb', line 132

def flag?
  type == :flag
end

#key?(name) ⇒ Boolean


Option Methods

Returns:

  • (Boolean)


124
125
126
# File 'lib/unimatrix_parser.rb', line 124

def key?( name )
  options.key?( name )
end

#longObject



170
171
172
# File 'lib/unimatrix_parser.rb', line 170

def long
  options[ :long ]
end

#multiObject Also known as: multi?



140
141
142
# File 'lib/unimatrix_parser.rb', line 140

def multi 
  options[ :multi ]
end

#multi_arg?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/unimatrix_parser.rb', line 146

def multi_arg?
  MULTI_ARG_TYPES.include?( type )
end

#required?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/unimatrix_parser.rb', line 178

def required?
  options[ :required ]
end

#shortObject



158
159
160
# File 'lib/unimatrix_parser.rb', line 158

def short
  options[ :short ]
end

#short=(value) ⇒ Object



166
167
168
# File 'lib/unimatrix_parser.rb', line 166

def short=( value )
  options[ :short ] = value
end

#short?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/unimatrix_parser.rb', line 162

def short?
  short && short != :none
end

#single_arg?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/unimatrix_parser.rb', line 136

def single_arg?
  SINGLE_ARG_TYPES.include?( type )
end

#typeObject



128
129
130
# File 'lib/unimatrix_parser.rb', line 128

def type
  options[ :type ]
end