Class: Consoler::Option

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

Overview

Represents an option

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#is_argumentBoolean (readonly)

Is the option an argument

Returns:

  • (Boolean)

    the current value of is_argument



13
14
15
# File 'lib/consoler/option.rb', line 13

def is_argument
  @is_argument
end

#is_longBoolean (readonly)

Is the option long (--option)

Returns:

  • (Boolean)

    the current value of is_long



13
14
15
# File 'lib/consoler/option.rb', line 13

def is_long
  @is_long
end

#is_optionalInteger (readonly)

Is the option optional (> 0) ([option])

Returns:

  • (Integer)

    the current value of is_optional



13
14
15
# File 'lib/consoler/option.rb', line 13

def is_optional
  @is_optional
end

#is_shortBoolean (readonly)

Is the option short (-o)

Returns:

  • (Boolean)

    the current value of is_short



13
14
15
# File 'lib/consoler/option.rb', line 13

def is_short
  @is_short
end

#is_valueBoolean (readonly)

Does the option need a value (--option=)

Returns:

  • (Boolean)

    the current value of is_value



13
14
15
# File 'lib/consoler/option.rb', line 13

def is_value
  @is_value
end

#nameString (readonly)

Name of the options

Returns:

  • (String)

    the current value of name



13
14
15
# File 'lib/consoler/option.rb', line 13

def name
  @name
end

Class Method Details

.create(option_def, tracker) ⇒ Object

Create a option

Yields an option for every option detected

Parameters:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/consoler/option.rb', line 27

def self.create(option_def, tracker)
  option = Option.new option_def, tracker

  if option.is_short and option.name.size > 1 then
    old_tracking = tracker.is_tracking
    old_is_value = option.is_value

    if option.is_optional then
      tracker.is_tracking = true
    end

    names = option.name.split('')

    names.each_with_index do |name, i|
      new_name = "-#{name}"

      if old_is_value and i == names.count - 1 then
        new_name = "#{new_name}="
      end

      yield Option.new new_name, tracker
    end

    tracker.is_tracking = old_tracking
  else
    yield option
  end
end

Instance Method Details

#default_valuenil | 0 | false

Get the default value of this option

Returns:

  • (nil | 0 | false)


81
82
83
84
85
86
87
# File 'lib/consoler/option.rb', line 81

def default_value
  return nil if is_value
  return 0 if is_short
  return false if is_long

  return nil
end

#to_definitionString

Get the definition of the option

Does not include the optional information, as that is linked to other options

Returns:

  • (String)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/consoler/option.rb', line 62

def to_definition
  definition = name

  if is_long then
    definition = "--#{definition}"
  elsif is_short then
    definition = "-#{definition}"
  end

  if is_value then
    definition = "#{definition}="
  end

  definition
end