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_repeating_arguments, #to_str

Constructor Details

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

Returns a new instance of Option


242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/docopt.rb', line 242

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


240
241
242
# File 'lib/docopt.rb', line 240

def argcount
  @argcount
end

#longObject (readonly)

Returns the value of attribute long


239
240
241
# File 'lib/docopt.rb', line 239

def long
  @long
end

#shortObject (readonly)

Returns the value of attribute short


239
240
241
# File 'lib/docopt.rb', line 239

def short
  @short
end

Class Method Details

.parse(option_description) ⇒ Object


257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/docopt.rb', line 257

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

  options = options.gsub(',', ' ').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
  new(short, long, argcount, value)
end

Instance Method Details

#inspectObject


292
293
294
# File 'lib/docopt.rb', line 292

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

#nameObject


288
289
290
# File 'lib/docopt.rb', line 288

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

#single_match(left) ⇒ Object


279
280
281
282
283
284
285
286
# File 'lib/docopt.rb', line 279

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