Class: PVN::Command::Documentation

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

Constant Summary collapse

SUMMARY_RE =
Regexp.new '^(?:(.*)(?:NOWRAP(.*)/NOWRAP)|(.*))(.*)$', Regexp::MULTILINE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Documentation

Returns a new instance of Documentation.



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

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.



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

def description
  @description
end

#examplesObject (readonly)

Returns the value of attribute examples.



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

def examples
  @examples
end

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/pvn/command/doc.rb', line 20

def options
  @options
end

#summaryObject

Returns the value of attribute summary.



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

def summary
  @summary
end

#usageObject

Returns the value of attribute usage.



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

def usage
  @usage
end

Instance Method Details

#example_to_doc(ex, out) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/pvn/command/doc.rb', line 99

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

#option_to_doc(opt, out) ⇒ Object



110
111
112
# File 'lib/pvn/command/doc.rb', line 110

def option_to_doc opt, out
  opt.to_doc out
end

#subcommands(args = nil) ⇒ Object



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

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

#subcommands=(args) ⇒ Object



41
42
43
# File 'lib/pvn/command/doc.rb', line 41

def subcommands= args
  @subcommands = args
end

#wrap(out, str) ⇒ Object



45
46
47
48
# File 'lib/pvn/command/doc.rb', line 45

def wrap out, str
  # I could use tput cols; ENV['COLUMNS'] works in irb, not in Ruby scripts.
  out.puts format_text str.gsub("\n", ''), 78, 2
end

#write(out = $io) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pvn/command/doc.rb', line 65

def write out = $io
  subcmds = @subcommands

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

  out.puts subcmdstr + ": " + @description
  out.puts "usage: " + subcmds[0] + " " + @usage
  out.puts ""
  write_summary out
  
  write_section "options", @options, out do |opt, io|
    option_to_doc opt, io
  end

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

#write_section(name, section, out) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pvn/command/doc.rb', line 87

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

#write_summary(out) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pvn/command/doc.rb', line 50

def write_summary out
  summary.join("\n").scan(SUMMARY_RE) do |md|
    if md[2]
      wrap out, md[2]
      next
    end

    wrap out, md[0]
    out.puts md[1].collect { |x| "  " + x }
    
    out.puts
    wrap out, md[3]
  end
end