Class: Rubycli::HelpRenderer

Inherits:
Object
  • Object
show all
Includes:
TypeUtils
Defined in:
lib/rubycli/help_renderer.rb

Instance Method Summary collapse

Methods included from TypeUtils

analyze_placeholder, boolean_string?, boolean_type?, convert_boolean, default_placeholder_for, determine_requires_value, infer_types_from_placeholder, nil_type?, normalize_long_option, normalize_short_option, normalize_type_list, normalize_type_token, parse_list

Constructor Details

#initialize(documentation_registry:) ⇒ HelpRenderer

Returns a new instance of HelpRenderer.



9
10
11
# File 'lib/rubycli/help_renderer.rb', line 9

def initialize(documentation_registry:)
  @documentation_registry = documentation_registry
end

Instance Method Details

#help_text(_target, catalog) ⇒ Object



19
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
48
49
# File 'lib/rubycli/help_renderer.rb', line 19

def help_text(_target, catalog)
  lines = ["Usage: #{File.basename($PROGRAM_NAME)} COMMAND [arguments]", '']

  instance_entries = catalog.entries_for(:instance)
  class_entries = catalog.entries_for(:class)
  groups = []
  groups << { label: 'Instance methods', entries: instance_entries } unless instance_entries.empty?
  groups << { label: 'Class methods', entries: class_entries } unless class_entries.empty?

  return (lines << 'No commands available.').join("\n") if groups.empty?

  lines << 'Available commands:'
  groups.each do |group|
    lines << "  #{group[:label]}:"
    group[:entries].each do |entry|
      description = method_description(entry.method)
      line = "    #{entry.command.ljust(20)}"
      line += " #{description}" unless description.empty?
      lines << line.rstrip

      lines << "      Aliases: #{entry.aliases.join(', ')}" unless entry.aliases.empty?
    end
    lines << '' unless group.equal?(groups.last)
  end

  lines << 'Methods with the same name can be invoked via instance::NAME / class::NAME.' if catalog.duplicates.any?

  lines << ''
  lines << "Detailed command help: #{File.basename($PROGRAM_NAME)} COMMAND help"
  lines.join("\n")
end

#method_description(method_obj) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/rubycli/help_renderer.rb', line 51

def method_description(method_obj)
   = @documentation_registry.(method_obj)
  summary = [:summary]
  return summary if summary && !summary.empty?

  params_str = format_method_parameters(method_obj.parameters, )
  params_str.empty? ? '(no arguments)' : params_str
end

io is $stderr when the help is shown because a command failed, so that piping stdout to another program never mixes diagnostics into the payload.



15
16
17
# File 'lib/rubycli/help_renderer.rb', line 15

def print_help(target, catalog, io: $stdout)
  io.puts(help_text(target, catalog))
end

#usage_for_method(command, method) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rubycli/help_renderer.rb', line 60

def usage_for_method(command, method)
   = @documentation_registry.(method)
  params_str = format_method_parameters(method.parameters, )
  usage_lines = ["Usage: #{File.basename($PROGRAM_NAME)} #{command} #{params_str}".strip]

  options = [:options] || []
  positionals_in_order = ordered_positionals(method, )

  usage_lines.concat(render_positionals(positionals_in_order)) if positionals_in_order.any?
  usage_lines.concat(render_options(options, required_keyword_names(method))) if options.any?

  returns = [:returns] || []
  if returns.any?
    usage_lines << '' unless usage_lines.last == ''
    usage_lines << 'Return values:'
    returns.each do |ret|
      type_label = (ret.types || []).join(' | ')
      line = "  #{type_label}"
      line += "  #{ret.description}" if ret.description && !ret.description.empty?
      usage_lines << line
    end
  end

  usage_lines.pop while usage_lines.last == ''
  usage_block = usage_lines.join("\n")

  sections = []
  summary_lines = [:summary_lines] || []
  summary_block = summary_lines.join("\n").rstrip
  sections << summary_block unless summary_block.empty?
  sections << usage_block unless usage_block.empty?
  detail_lines = [:detail_lines] || []
  detail_block = detail_lines.join("\n").rstrip
  sections << detail_block unless detail_block.empty?

  sections.join("\n\n")
end