Class: EacCli::RunnerWith::Help::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_cli/runner_with/help/builder.rb,
lib/eac_cli/runner_with/help/builder/alternative.rb

Defined Under Namespace

Classes: Alternative

Constant Summary collapse

SEP =
' '
IDENT =
SEP * 2
OPTION_DESC_SEP =
IDENT * 2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.option_long(option) ⇒ Object



17
18
19
20
21
# File 'lib/eac_cli/runner_with/help/builder.rb', line 17

def option_long(option)
  b = option.long
  b += '=<value>' if option.argument?
  b
end

.option_short(option) ⇒ Object



23
24
25
26
27
# File 'lib/eac_cli/runner_with/help/builder.rb', line 23

def option_short(option)
  b = option.short
  b += '=<value>' if option.argument?
  b
end

.option_usage_full(option) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/eac_cli/runner_with/help/builder.rb', line 29

def option_usage_full(option)
  if option.long.present?
    [option.short, option_long(option)].reject(&:blank?).join(word_separator)
  else
    option_short(option)
  end
end

.word_separatorObject



37
38
39
# File 'lib/eac_cli/runner_with/help/builder.rb', line 37

def word_separator
  SEP
end

Instance Method Details

#definitionObject



44
45
46
# File 'lib/eac_cli/runner_with/help/builder.rb', line 44

def definition
  runner.class.runner_definition
end

#option_definition(option) ⇒ Object



48
49
50
51
52
# File 'lib/eac_cli/runner_with/help/builder.rb', line 48

def option_definition(option)
  [self.class.option_usage_full(option), option.description,
   option.default_value? ? "[Default: \"#{option.default_value}\"]" : nil]
    .reject(&:blank?).join(OPTION_DESC_SEP)
end

#options_sectionObject



63
64
65
66
67
# File 'lib/eac_cli/runner_with/help/builder.rb', line 63

def options_section
  "Options:\n" +
    definition.alternatives.flat_map(&:options)
      .map { |option| IDENT + option_definition(option) + "\n" }.join
end

#section(header, include_header = true) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/eac_cli/runner_with/help/builder.rb', line 54

def section(header, include_header = true)
  b = include_header ? "#{header.humanize}:\n" : ''
  b += send("self_#{header}") + "\n"
  definition.alternatives.each do |alternative|
    b += IDENT + self.alternative(alternative) + "\n"
  end
  b
end

#to_sObject



76
77
78
# File 'lib/eac_cli/runner_with/help/builder.rb', line 76

def to_s
  "#{definition.description}\n\n#{usage_section}\n#{options_section}\n"
end

#usage_sectionObject



69
70
71
72
73
74
# File 'lib/eac_cli/runner_with/help/builder.rb', line 69

def usage_section
  "Usage:\n" +
    definition.alternatives.map do |alternative|
      IDENT + self.alternative(alternative) + "\n"
    end.join
end