Class: PVN::Command::Documentation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Documentation

Returns a new instance of Documentation.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pvn/command/doc.rb', line 20

def initialize(&blk)
  @subcommands = nil
  @description = nil
  @usage = nil
  @summary = nil
  @options = Array.new
  @examples = Array.new
  if blk
    blk.call
  end
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



13
14
15
# File 'lib/pvn/command/doc.rb', line 13

def description
  @description
end

#examplesObject (readonly)

Returns the value of attribute examples.



17
18
19
# File 'lib/pvn/command/doc.rb', line 17

def examples
  @examples
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/pvn/command/doc.rb', line 18

def options
  @options
end

#summaryObject

Returns the value of attribute summary.



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

def summary
  @summary
end

#usageObject

Returns the value of attribute usage.



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

def usage
  @usage
end

Instance Method Details

#example_to_doc(ex, io) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/pvn/command/doc.rb', line 79

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



90
91
92
# File 'lib/pvn/command/doc.rb', line 90

def option_to_doc opt, io
  opt.to_doc io
end

#subcommands(args = nil) ⇒ Object



32
33
34
35
36
37
# File 'lib/pvn/command/doc.rb', line 32

def subcommands args = nil
  if args
    @subcommands = args
  end
  @subcommands
end

#subcommands=(args) ⇒ Object



39
40
41
# File 'lib/pvn/command/doc.rb', line 39

def subcommands= args
  @subcommands = args
end

#write(io = $stdout) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pvn/command/doc.rb', line 43

def write 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



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pvn/command/doc.rb', line 67

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