class TomParse::Argument

Encapsulate a method argument.

TODO: Does not yet support default parameter.

Attributes

description[RW]
name[RW]
options[RW]

Public Class Methods

new(name, description = '') click to toggle source

Create new Argument object.

name - name of argument description - argument description

# File lib/tomparse/argument.rb, line 20
def initialize(name, description = '')
  @name = name.to_s.intern
  parse(description)
end

Public Instance Methods

optional?() click to toggle source

Is this an optional argument?

Returns Boolean.

# File lib/tomparse/argument.rb, line 28
def optional?
  @optional
end
parse(description) click to toggle source

Parse arguments section. Arguments occur subsequent to the description.

section - String containing argument definitions.

Returns nothing.

# File lib/tomparse/argument.rb, line 38
def parse(description)
  desc = []
  opts = []

  lines = description.lines.to_a

  until lines.empty? or /^\s+\:(\w+)\s+-\s+(.*?)$/ =~ lines.first
    desc << lines.shift.chomp.squeeze(" ")
  end

  opts = []
  last_indent = nil

  lines.each do |line|
    next if line.strip.empty?
    indent = line.scan(/^\s*/)[0].to_s.size

    if last_indent && indent > last_indent
      opts.last.description << line.squeeze(" ")
    else
      param, d = line.split(" - ")
      opts << Option.new(param.strip, d.strip) if param && d
    end

    last_indent = indent
  end

  # Look for `(optional)` at end of description. If found, mark argument
  # as @optional and remove from description.
  #if md = /(\(optional\)\s*)(?:\[|\.\Z|\Z)/.match(desc.last)
  #  @optional = true
  #  desc.last[*md.offset(1)] = ''
  #end

  # Join the desc lines back together and ensure no extraneous whitespace.
  text = desc.join.strip

  # If the description contains the word "optional" the argument is taken
  # to be optional. Note, I think this probably should be `(optional)` to
  # prevent false positives, but the spec suggests otherwise.
  if /\boptional\b/ =~ text
    @optional = true
  end

  @description = text
  @options = opts
end