Class: Synoption::BaseOption

Inherits:
Object
  • Object
show all
Includes:
Logue::Loggable
Defined in:
lib/synoption/base_option.rb

Direct Known Subclasses

Option

Constant Summary collapse

NO_CMDLINE_OPTION =

for as_cmdline_option:

Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, tag, description, default, options = Hash.new) ⇒ BaseOption

Returns a new instance of BaseOption.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/synoption/base_option.rb', line 25

def initialize name, tag, description, default, options = Hash.new
  @name = name
  @tag = tag
  @description = description

  @value = @default = default

  @matchers = Matchers.new @tag, @name, options[:negate], options[:regexp]

  @negate = options[:negate]
  @regexp = options[:regexp]
  
  if options.include? :as_cmdline_option
    if options[:as_cmdline_option].nil?
      @as_cmdline_option = NO_CMDLINE_OPTION
    else
      @as_cmdline_option = options[:as_cmdline_option]
    end
  else
    @as_cmdline_option = nil
  end

  @unsets = options[:unsets]
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



20
21
22
# File 'lib/synoption/base_option.rb', line 20

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



19
20
21
# File 'lib/synoption/base_option.rb', line 19

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/synoption/base_option.rb', line 17

def name
  @name
end

#negateObject (readonly)

Returns the value of attribute negate.



22
23
24
# File 'lib/synoption/base_option.rb', line 22

def negate
  @negate
end

#regexpObject (readonly)

Returns the value of attribute regexp.



23
24
25
# File 'lib/synoption/base_option.rb', line 23

def regexp
  @regexp
end

#tagObject (readonly)

Returns the value of attribute tag.



18
19
20
# File 'lib/synoption/base_option.rb', line 18

def tag
  @tag
end

Instance Method Details

#exact_match?(arg) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/synoption/base_option.rb', line 68

def exact_match? arg
  @matchers.exact.match? arg
end

#negative_match?(arg) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/synoption/base_option.rb', line 72

def negative_match? arg
  @matchers.negative and @matchers.negative.match? arg
end

#next_argument(args) ⇒ Object



97
98
99
100
# File 'lib/synoption/base_option.rb', line 97

def next_argument args
  raise "ERROR: option #{name} expecting following argument" if args.empty?
  args.shift
end

#post_process(option_set, unprocessed) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/synoption/base_option.rb', line 121

def post_process option_set, unprocessed
  resolve_value option_set, unprocessed

  if @unsets
    option_set.unset @unsets
  end
end

#process(args) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/synoption/base_option.rb', line 102

def process args
  if @matchers.exact.match? args[0]
    args.shift
    val = takes_value? ? next_argument(args) : true
    set_value val
    true
  elsif @matchers.negative && @matchers.negative.match?(args[0])
    arg = args.shift
    set_value false
    true
  elsif @matchers.regexp && (md = @matchers.regexp.match?(args[0]))
    arg = args.shift
    set_value md[0]
    true
  else
    false
  end
end

#regexp_match?(arg) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/synoption/base_option.rb', line 76

def regexp_match? arg
  @matchers.regexp and @matchers.regexp.match? arg
end

#resolve_value(option_set, unprocessed) ⇒ Object



129
130
# File 'lib/synoption/base_option.rb', line 129

def resolve_value option_set, unprocessed
end

#set_value(val) ⇒ Object



84
85
86
# File 'lib/synoption/base_option.rb', line 84

def set_value val
  @value = val
end

#takes_value?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/synoption/base_option.rb', line 50

def takes_value?
  true
end

#to_command_lineObject



54
55
56
57
58
59
60
61
62
# File 'lib/synoption/base_option.rb', line 54

def to_command_line
  return nil unless value
  
  if @as_cmdline_option
    @as_cmdline_option == NO_CMDLINE_OPTION ? nil : @as_cmdline_option
  else
    [ tag, value ]
  end
end

#to_doc(io) ⇒ Object



92
93
94
95
# File 'lib/synoption/base_option.rb', line 92

def to_doc io
  doc = Doc.new self
  doc.to_doc io
end

#to_sObject



64
65
66
# File 'lib/synoption/base_option.rb', line 64

def to_s
  [ @name, @tag ].join(", ")
end

#unsetObject



80
81
82
# File 'lib/synoption/base_option.rb', line 80

def unset
  @value = nil
end

#valueObject



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

def value
  @value
end