Class: Runfile::DocoptMaker

Inherits:
Object
  • Object
show all
Defined in:
lib/runfile/docopt_maker.rb

Overview

The DocoptMaker class handles the dynamic generation of the docopt document.

Instance Method Summary collapse

Constructor Details

#initialize(name, version, summary, actions, options) ⇒ DocoptMaker

Returns a new instance of DocoptMaker.



10
11
12
13
14
15
16
# File 'lib/runfile/docopt_maker.rb', line 10

def initialize(name, version, summary, actions, options)
  @name    = name
  @version = version
  @summary = summary
  @actions = actions
  @options = options
end

Instance Method Details

#makeObject

Generate a document based on all the actions, help messages and options we have collected from the Runfile DSL.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/runfile/docopt_maker.rb', line 20

def make
  width, height = detect_terminal_size
  doc = "#{@name} #{@version}\n"
  doc += "#{@summary} \n" if @summary
  doc += "\nUsage:\n";
  @actions.each do |name, action|
    doc += "  run #{action.usage}\n" unless action.usage == false
  end
  doc += "  run (-h | --help | --version)\n\n"
  caption_printed = false
  @actions.each do |name, action|
    action.help or next
    doc += "Commands:\n" unless caption_printed
    caption_printed = true
    helpline = "      #{action.help}"
    wrapped  = word_wrap helpline, width
    doc += "  #{action.usage}\n#{wrapped}\n\n" unless action.usage == false
  end
  doc += "Options:\n"
  doc += "  -h --help\n      Show this screen\n\n"
  doc += "  --version\n      Show version\n\n"
  @options.each do |flag, text|
    helpline = "      #{text}"
    wrapped  = word_wrap helpline, width
    doc += "  #{flag}\n#{wrapped}\n\n"
  end
  doc
end