Class: UsageMod::Option

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

Overview

This class represents an option (something that starts with a ‘-’ or ‘–’) and its characteristics, such as:

  • whether it is required or not

  • its short and long names

  • its type if it has one (default is string)

  • the choices for it if it is an option with fixed number of choices

  • its description

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Option

Returns a new instance of Option.



207
208
209
210
211
212
213
214
215
# File 'lib/Usage.rb', line 207

def initialize(name)
  @name = name
  @long_name = nil
  @arg_name = nil
  @arg_type = nil
  @has_argument = nil
  @is_required = true
  @choices = Choices.new([])
end

Instance Attribute Details

#arg_nameObject

Returns the value of attribute arg_name.



205
206
207
# File 'lib/Usage.rb', line 205

def arg_name
  @arg_name
end

#arg_typeObject

Returns the value of attribute arg_type.



205
206
207
# File 'lib/Usage.rb', line 205

def arg_type
  @arg_type
end

#choicesObject

Returns the value of attribute choices.



206
207
208
# File 'lib/Usage.rb', line 206

def choices
  @choices
end

#descriptionObject

Returns the value of attribute description.



206
207
208
# File 'lib/Usage.rb', line 206

def description
  @description
end

#has_argumentObject

Returns the value of attribute has_argument.



206
207
208
# File 'lib/Usage.rb', line 206

def has_argument
  @has_argument
end

#is_requiredObject

Returns the value of attribute is_required.



205
206
207
# File 'lib/Usage.rb', line 205

def is_required
  @is_required
end

#long_nameObject

Returns the value of attribute long_name.



205
206
207
# File 'lib/Usage.rb', line 205

def long_name
  @long_name
end

#nameObject (readonly)

Returns the value of attribute name.



204
205
206
# File 'lib/Usage.rb', line 204

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object

allow it to be compared with other Option objects



227
228
229
# File 'lib/Usage.rb', line 227

def ==(other)
  @name = other.name
end

#hashObject

allow it to be used in a hash



220
221
222
# File 'lib/Usage.rb', line 220

def hash
  @name.hash
end

#long_name_as_symbolObject

the long option name as a symbol (ie. –long_name is :long_name)



241
242
243
# File 'lib/Usage.rb', line 241

def long_name_as_symbol
  @long_name.gsub(/-/, "_")
end

#name_as_symbolObject

the option name as a symbol (ie. :dash_x if -x is the option)



234
235
236
# File 'lib/Usage.rb', line 234

def name_as_symbol
  "dash_#{@name}"
end

#to_sObject



245
246
247
# File 'lib/Usage.rb', line 245

def to_s
  "OPTION:[#{@name}][#{@long_name}][#{@arg_type}][required=#{@is_required}]"
end