Class: Ragol::Option

Inherits:
Object
  • Object
show all
Includes:
Logue::Loggable
Defined in:
lib/ragol/option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = Hash.new, &blk) ⇒ Option

Returns a new instance of Option.



22
23
24
25
26
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
# File 'lib/ragol/option.rb', line 22

def initialize options = Hash.new, &blk
  @tags = nil
  @negates = nil
  @regexps = nil
  @name = nil
  @default = nil
  @unsets = nil
  @process = nil
  @takesvalue = nil
  @rcnames = nil
  @description = nil

  if blk
    blk.call self
  end

  tagsmatch = to_matcher(@tags || options[:tags])
  negatesmatch = to_matcher(@negates || options[:negates])
  regexpsmatch = to_matcher(@regexps || options[:regexps])

  @matchers = Ragol::Matchers.new tagsmatch, negatesmatch, regexpsmatch

  @name ||= options[:name] || @matchers.name

  @default ||= options[:default]
  @unsets ||= options[:unsets]
  @process ||= options[:process]
  @takesvalue ||= options[:takesvalue]
  @rcnames ||= [ options[:rcnames] ].flatten
  @description ||= options[:description]
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



16
17
18
# File 'lib/ragol/option.rb', line 16

def default
  @default
end

#descriptionObject

Returns the value of attribute description.



17
18
19
# File 'lib/ragol/option.rb', line 17

def description
  @description
end

#matchersObject (readonly)

Returns the value of attribute matchers.



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

def matchers
  @matchers
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/ragol/option.rb', line 15

def name
  @name
end

#negatesObject

Returns the value of attribute negates.



19
20
21
# File 'lib/ragol/option.rb', line 19

def negates
  @negates
end

#regexpsObject

Returns the value of attribute regexps.



20
21
22
# File 'lib/ragol/option.rb', line 20

def regexps
  @regexps
end

#tagsObject

Returns the value of attribute tags.



18
19
20
# File 'lib/ragol/option.rb', line 18

def tags
  @tags
end

Instance Method Details

#argument_missingObject



104
105
106
107
108
# File 'lib/ragol/option.rb', line 104

def argument_missing
  if takes_value? == true
    raise "value expected for option: #{self}"
  end
end

#convert(md) ⇒ Object



84
85
86
# File 'lib/ragol/option.rb', line 84

def convert md
  md.kind_of?(MatchData) ? md[-1] : md
end

#do_match(val) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/ragol/option.rb', line 88

def do_match val
  if valuere = value_regexp
    unless md = valuere.match(val)
      raise "invalid argument '#{val}' for option: #{self}"
    end
    md
  else
    val
  end
end

#match_next_value(results) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/ragol/option.rb', line 128

def match_next_value results
  if takes_value? == true
    match_next_value_required results
  else
    match_next_value_optional results
  end
end

#match_next_value_optional(results) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ragol/option.rb', line 115

def match_next_value_optional results
  return unless val = results.current_arg
  return true if val[0] == '-'
  return results.shift_arg unless valuere = value_regexp
  
  if md = valuere.match(results.current_arg)
    results.shift_arg
    md
  else
    true
  end
end

#match_next_value_required(results) ⇒ Object



110
111
112
113
# File 'lib/ragol/option.rb', line 110

def match_next_value_required results
  val = results.shift_arg
  val && do_match(val)
end

#match_rc?(field) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/ragol/option.rb', line 54

def match_rc? field
  @rcnames.include?(field)
end

#post_process(option_set, results, unprocessed) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/ragol/option.rb', line 66

def post_process option_set, results, unprocessed
  resolve_value option_set, results, unprocessed

  if @unsets
    option_set.unset results, @unsets
  end
end

#resolve_value(option_set, results, unprocessed) ⇒ Object



74
75
# File 'lib/ragol/option.rb', line 74

def resolve_value option_set, results, unprocessed
end

#set_option_value(md, arg, results) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/ragol/option.rb', line 154

def set_option_value md, arg, results
  value = md == true ? true : convert(md)
  if @process
    setargs = [ value, arg, results.unprocessed ][0 ... @process.arity]
    @process.call(*setargs)
  end
  results.set_value name, value
end

#set_value_for_tag(results, arg) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/ragol/option.rb', line 136

def set_value_for_tag results, arg
  md = if takes_value?
         take_eq_value(arg) || match_next_value(results) || argument_missing
       else
         true
       end
  set_option_value md, arg, results
end

#set_value_negative(results, arg) ⇒ Object



145
146
147
# File 'lib/ragol/option.rb', line 145

def set_value_negative results, arg
  set_option_value false, arg, results
end

#set_value_regexp(results, arg) ⇒ Object



149
150
151
152
# File 'lib/ragol/option.rb', line 149

def set_value_regexp results, arg
  md = @matchers.regexp_match? arg
  set_option_value md, arg, results
end

#take_eq_value(opt) ⇒ Object



99
100
101
102
# File 'lib/ragol/option.rb', line 99

def take_eq_value opt
  val = opt.split('=', 2)[1]
  val && do_match(val)
end

#takes_value?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ragol/option.rb', line 62

def takes_value?
  @takesvalue
end

#to_doc(io) ⇒ Object



163
164
165
166
# File 'lib/ragol/option.rb', line 163

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

#to_matcher(elements) ⇒ Object



58
59
60
# File 'lib/ragol/option.rb', line 58

def to_matcher elements
  elements && Ragol::Matcher.new(elements)
end

#to_sObject



77
78
79
# File 'lib/ragol/option.rb', line 77

def to_s
  @matchers.to_s
end

#value_regexpObject



81
82
# File 'lib/ragol/option.rb', line 81

def value_regexp
end