Class: Runfile::DocoptHelper

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

Overview

The DocoptHelper class handles the dynamic generation of the docopt document and the docopt part of the execution (meaning, to call Docopt so it returns the parsed arguments or halts with usage message).

Instance Method Summary collapse

Constructor Details

#initialize(superspace, name, version, summary, actions, options) ⇒ DocoptHelper

The constructor expects to get all the textual details needed to generate a docopt document (name, version, summary, options) and an array of Action objects. The superspace argument will be the name of runfile, in case we are running a named.runfile. It is only needed to generate the proper ‘run superspace (-h|–help|–version)` line



19
20
21
22
23
24
25
26
# File 'lib/runfile/docopt_helper.rb', line 19

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

Instance Method Details

#args(argv) ⇒ Object

Call the docopt handler, which will either return a parsed arguments list, or halt execution and show usage.



89
90
91
# File 'lib/runfile/docopt_helper.rb', line 89

def args(argv)
  Docopt::docopt(docopt, version: @version, argv:argv)
end

#docoptObject

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



30
31
32
33
34
35
36
37
38
# File 'lib/runfile/docopt_helper.rb', line 30

def docopt
  width, height = detect_terminal_size
  doc = ["#{@name} #{@version}"]
  doc << "#{@summary}" if @summary
  doc += docopt_usage
  doc += docopt_commands width
  doc += docopt_options width
  doc.join "\n"
end

#docopt_commands(width) ⇒ Object

Return all docopt lines for the ‘Commands’ section



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/runfile/docopt_helper.rb', line 55

def docopt_commands(width)
  doc = []
  caption_printed = false
  @actions.each do |name, action|
    action.help or next
    doc << "Commands:" unless caption_printed
    caption_printed = true
    helpline = "      #{action.help}"
    wrapped  = word_wrap helpline, width
    doc << "  #{action.usage}\n#{wrapped}\n" unless action.usage == false
  end
  doc
end

#docopt_options(width) ⇒ Object

Return all docopt lines for the various ‘Options’ sections



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/runfile/docopt_helper.rb', line 70

def docopt_options(width)
  @options['Options'] = {} unless @options['Options']
  @options['Options']['-h --help'] = 'Show this screen'
  @options['Options']['--version'] = 'Show version number'

  doc = []
  @options.each do |scope, values|
    doc << "#{scope}:"
    values.each do |flag, text|
      helpline = "      #{text}"
      wrapped  = word_wrap helpline, width
      doc << "  #{flag}\n#{wrapped}\n"
    end
  end
  doc
end

#docopt_usageObject

Return all docopt lines for the ‘Usage’ section



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/runfile/docopt_helper.rb', line 41

def docopt_usage 
  doc = ["\nUsage:"];
  @actions.each do |name, action|
    doc << "  run #{action.usage}" unless action.usage == false
  end
  if @superspace
    doc << "  run #{@superspace} (-h|--help|--version)\n"
  else
    doc << "  run (-h|--help|--version)\n"
  end
  doc
end