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

OPTION_DESC_SEP =
::EacCli::RunnerWith::Help::ListSection::IDENTATION * 2
SECTION_SEPARATOR =
"\n"

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
  ::EacCli::RunnerWith::Help::ListSection::WORD_SEPARATOR
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

#extra_sectionString?

Returns:

  • (String, nil)


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

def extra_section
  ess = extra_sections
  return nil if ess.none?

  ess.join(SECTION_SEPARATOR)
end

#extra_sectionsEnumerable<String>

Returns:

  • (Enumerable<String>)


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/eac_cli/runner_with/help/builder.rb', line 57

def extra_sections
  runner.if_respond(:help_extra_text, []) do |v|
    if v.is_a?(::Hash)
      v.map do |title, lines|
        ::EacCli::RunnerWith::Help::ListSection.new(title, lines)
      end
    else
      [v.to_s]
    end
  end
end

#option_definition(option) ⇒ Object



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

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_sectionString

Returns:

  • (String)


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

def options_section
  ::EacCli::RunnerWith::Help::ListSection.new(
    'Options',
    definition.alternatives.flat_map(&:options).map { |option| option_definition(option) }
  )
end

#to_sObject



91
92
93
94
95
# File 'lib/eac_cli/runner_with/help/builder.rb', line 91

def to_s
  r = ["#{definition.description}\n", usage_section, options_section]
        .map { |s| "#{s}#{SECTION_SEPARATOR}" }.join
  extra_section.if_present(r) { |v| r + v }
end

#usage_sectionString

Returns:

  • (String)


84
85
86
87
88
89
# File 'lib/eac_cli/runner_with/help/builder.rb', line 84

def usage_section
  ::EacCli::RunnerWith::Help::ListSection.new(
    'Usage',
    definition.alternatives.map { |alternative| self.alternative(alternative) }
  )
end