Class: Docopt::Option

Inherits:
ChildPattern show all
Defined in:
lib/docopt.rb

Instance Attribute Summary collapse

Attributes inherited from ChildPattern

#value

Attributes inherited from Pattern

#children

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ChildPattern

#flat, #match

Methods inherited from Pattern

#==, #dump, #either, #fix, #fix_identities, #fix_list_arguments, #to_str

Constructor Details

#initialize(short = nil, long = nil, argcount = 0, value = false) ⇒ Option

Returns a new instance of Option.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/docopt.rb', line 219

def initialize(short=nil, long=nil, argcount=0, value=false)
  unless [0, 1].include? argcount
    raise RuntimeError
  end

  @short, @long = short, long
  @argcount, @value = argcount, value

  if value == false and argcount > 0
    @value = nil
  else
    @value = value
  end
end

Instance Attribute Details

#argcountObject

Returns the value of attribute argcount.



217
218
219
# File 'lib/docopt.rb', line 217

def argcount
  @argcount
end

#longObject (readonly)

Returns the value of attribute long.



216
217
218
# File 'lib/docopt.rb', line 216

def long
  @long
end

#shortObject (readonly)

Returns the value of attribute short.



216
217
218
# File 'lib/docopt.rb', line 216

def short
  @short
end

Class Method Details

.parse(option_description) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/docopt.rb', line 234

def self.parse(option_description)
  short, long, argcount, value = nil, nil, 0, false
  options, _, description = option_description.strip.partition('  ')

  options.gsub!(",", " ")
  options.gsub!("=", " ")

  for s in options.split
    if s.start_with?('--')
      long = s
    elsif s.start_with?('-')
      short = s
    else
      argcount = 1
    end
  end
  if argcount > 0
    matched = description.scan(/\[default: (.*)\]/i)
    value = matched[0][0] if matched.count > 0
  end
  ret = self.new(short, long, argcount, value)
  return ret
end

Instance Method Details

#inspectObject



271
272
273
# File 'lib/docopt.rb', line 271

def inspect
  return "Option(#{self.short}, #{self.long}, #{self.argcount}, #{self.value})"
end

#nameObject



267
268
269
# File 'lib/docopt.rb', line 267

def name
  return self.long ? self.long : self.short
end

#single_match(left) ⇒ Object



258
259
260
261
262
263
264
265
# File 'lib/docopt.rb', line 258

def single_match(left)
  left.each_with_index do |p, n|
    if self.name == p.name
      return [n, p]
    end
  end
  return [nil, nil]
end