Class: EacCli::Definition::HelpFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_cli/definition/help_formatter.rb

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



13
14
15
16
17
# File 'lib/eac_cli/definition/help_formatter.rb', line 13

def option_long(option)
  b = option.long
  b += '=VALUE' if option.argument?
  b
end

.option_short(option) ⇒ Object



19
20
21
22
23
# File 'lib/eac_cli/definition/help_formatter.rb', line 19

def option_short(option)
  b = option.short
  b += 'VALUE' if option.argument?
  b
end

Instance Method Details

#positional_argument(positional) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/eac_cli/definition/help_formatter.rb', line 28

def positional_argument(positional)
  if positional.subcommand?
    ::EacCli::DocoptRunner::SUBCOMMANDS_MACRO
  else
    r = "<#{positional.name}>"
    r += '...' if positional.repeat?
    r = "[#{r}]" if positional.optional?
    r
  end
end

#section(header, include_header = true) ⇒ Object



39
40
41
42
43
44
# File 'lib/eac_cli/definition/help_formatter.rb', line 39

def section(header, include_header = true)
  b = include_header ? "#{header.humanize}:\n" : ''
  b += send("self_#{header}") + "\n"
  # TO-DO: implement alternatives
  b
end

#self_optionsObject



46
47
48
# File 'lib/eac_cli/definition/help_formatter.rb', line 46

def self_options
  definition.options.map { |option| IDENT + option_definition(option) }.join("\n")
end

#self_usageObject



50
51
52
# File 'lib/eac_cli/definition/help_formatter.rb', line 50

def self_usage
  IDENT + self_usage_arguments.join(SEP)
end

#self_usage_argumentsObject



54
55
56
57
58
59
# File 'lib/eac_cli/definition/help_formatter.rb', line 54

def self_usage_arguments
  [::EacCli::DocoptRunner::PROGRAM_MACRO] +
    definition.options_argument.if_present([]) { |_v| ['[options]'] } +
    self_usage_arguments_options +
    self_usage_arguments_positional
end

#self_usage_arguments_optionsObject



61
62
63
64
65
# File 'lib/eac_cli/definition/help_formatter.rb', line 61

def self_usage_arguments_options
  definition.options.select(&:show_on_usage?).map do |option|
    self.class.option_long(option)
  end
end

#self_usage_arguments_positionalObject



67
68
69
# File 'lib/eac_cli/definition/help_formatter.rb', line 67

def self_usage_arguments_positional
  definition.positional.map { |p| positional_argument(p) }
end

#to_bannerObject



71
72
73
# File 'lib/eac_cli/definition/help_formatter.rb', line 71

def to_banner
  "#{definition.description}\n\n#{section('usage')}"
end