Class: TomParse::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/tomparse/argument.rb

Overview

TODO:

Does not yet support default parameter.

Encapsulate a method argument.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description = '') ⇒ Argument

Create new Argument object.

Parameters:

  • name

    name of argument

  • description (defaults to: '')

    argument description



20
21
22
23
# File 'lib/tomparse/argument.rb', line 20

def initialize(name, description = '')
  @name = name.to_s.intern
  parse(description)
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/tomparse/argument.rb', line 11

def description
  @description
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/tomparse/argument.rb', line 9

def name
  @name
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/tomparse/argument.rb', line 13

def options
  @options
end

Instance Method Details

#optional?Boolean

Is this an optional argument?

Returns:

  • (Boolean)

    Boolean.



28
29
30
# File 'lib/tomparse/argument.rb', line 28

def optional?
  @optional
end

#parse(description) ⇒ void

This method returns an undefined value.

Parse arguments section. Arguments occur subsequent to the description.

Parameters:

  • section

    String containing argument definitions.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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