Class: Exec::CommandOption::Option

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

Overview

This subclass represents a command option.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(short_name, long_name, description, mandatory = false, require_value = false, check_format = nil) ⇒ Option

Returns a new instance of Option.

Raises:

  • (NameError)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/exec/command_option.rb', line 180

def initialize(short_name, long_name, description, mandatory = false, require_value = false, check_format = nil)

  raise NameError.new("Short value has to be a String of one character") unless short_name.instance_of?(String) && short_name.length == 1
  raise NameError.new("Long value has to be a non empty String") unless long_name.instance_of?(String) && long_name.length > 0
  raise NameError.new("Description value has to be a non empty String") unless description.instance_of?(String) && description.length > 0
  raise NameError.new("Check format method has to be of type Method class which get one string and returns a boolean,String.") unless check_format.nil? || (check_format.instance_of?(Method) && check_format.arity == 1)

  @short_name = short_name
  @long_name = long_name
  @description = description
  @require_value = require_value
  @mandatory = mandatory
  @check_format = check_format
end

Instance Attribute Details

#check_formatObject (readonly)

Returns the value of attribute check_format.



172
173
174
# File 'lib/exec/command_option.rb', line 172

def check_format
  @check_format
end

#descriptionObject (readonly)

Returns the value of attribute description.



169
170
171
# File 'lib/exec/command_option.rb', line 169

def description
  @description
end

#long_nameObject (readonly)

Returns the value of attribute long_name.



168
169
170
# File 'lib/exec/command_option.rb', line 168

def long_name
  @long_name
end

#mandatoryObject (readonly)

Returns the value of attribute mandatory.



171
172
173
# File 'lib/exec/command_option.rb', line 171

def mandatory
  @mandatory
end

#require_valueObject (readonly)

Returns the value of attribute require_value.



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

def require_value
  @require_value
end

#short_nameObject (readonly)

Returns the value of attribute short_name.



167
168
169
# File 'lib/exec/command_option.rb', line 167

def short_name
  @short_name
end

Instance Method Details

#to_usage_sObject

Returns A string that describes option for usage generating.

Returns:

  • A string that describes option for usage generating



211
212
213
214
215
216
217
218
# File 'lib/exec/command_option.rb', line 211

def to_usage_s
  s = ""
  s += "[" unless @mandatory
  s += "-" + @short_name
  s += " " + value_name if @require_value
  s += "]" unless @mandatory
  return s
end

#valid_value?(value) ⇒ Boolean

Returns true if given value is valid.

Returns:

  • (Boolean)

    true if given value is valid



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/exec/command_option.rb', line 197

def valid_value?(value)
  msg = ""

  if require_value
    valid, msg = check_format.call(value)
  else
    valid = value == true || value == false
  end

  return valid, msg
end

#value_nameObject

Adds _NAME to the long_name.



175
176
177
# File 'lib/exec/command_option.rb', line 175

def value_name
  @long_name.upcase + "_NAME"
end