Class: PVN::CommandDoc

Inherits:
Object
  • Object
show all
Defined in:
lib/pvn/cmddoc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandDoc

Returns a new instance of CommandDoc.



17
18
19
20
21
22
23
24
# File 'lib/pvn/cmddoc.rb', line 17

def initialize
  @subcommands = nil
  @description = nil
  @usage = nil
  @summary = nil
  @options = Array.new
  @examples = Array.new
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



10
11
12
# File 'lib/pvn/cmddoc.rb', line 10

def description
  @description
end

#examplesObject (readonly)

Returns the value of attribute examples.



14
15
16
# File 'lib/pvn/cmddoc.rb', line 14

def examples
  @examples
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/pvn/cmddoc.rb', line 15

def options
  @options
end

#subcommandsObject

Returns the value of attribute subcommands.



9
10
11
# File 'lib/pvn/cmddoc.rb', line 9

def subcommands
  @subcommands
end

#summaryObject

Returns the value of attribute summary.



12
13
14
# File 'lib/pvn/cmddoc.rb', line 12

def summary
  @summary
end

#usageObject

Returns the value of attribute usage.



11
12
13
# File 'lib/pvn/cmddoc.rb', line 11

def usage
  @usage
end

Instance Method Details

#example_to_doc(ex, io) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/pvn/cmddoc.rb', line 62

def example_to_doc ex, io
  ex.each_with_index do |line, idx|
    if idx == 0
      io.puts "  % #{line}"
    else
      io.puts "    #{line}"
    end
  end
  io.puts
end

#option_to_doc(opt, io) ⇒ Object



73
74
75
# File 'lib/pvn/cmddoc.rb', line 73

def option_to_doc opt, io
  opt.to_doc io
end

#to_doc(io = $stdout) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pvn/cmddoc.rb', line 26

def to_doc io = $stdout
  doc = Array.new

  subcmds = @subcommands

  subcmdstr = subcmds[0].dup
  if subcmds.size > 1
    subcmdstr << " (" << subcmds[1 .. -1].join(" ") << ")"
  end

  io.puts subcmdstr + ": " + @description
  io.puts "usage: " + subcmds[0] + " " + @usage
  io.puts ""
  io.puts @summary.collect { |line| "  " + line }

  write_section "options", @options, io do |opt, io|
    option_to_doc opt, io
  end

  write_section "examples", @examples, io do |ex, io|
    example_to_doc ex, io
  end
end

#write_section(name, section, io) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pvn/cmddoc.rb', line 50

def write_section name, section, io
  if section.length > 0
    io.puts ""
    io.puts "#{name.capitalize}:"
    io.puts ""
    
    section.each do |opt|
      yield opt, io
    end
  end
end