class Toys::Utils::HelpText::UsageStringAssembler

@private

Attributes

result[R]

Public Class Methods

new(tool, executable_name, subtools, separate_sources, indent, left_column_width, wrap_width) click to toggle source
# File lib/toys/utils/help_text.rb, line 205
def initialize(tool, executable_name, subtools, separate_sources,
               indent, left_column_width, wrap_width)
  @tool = tool
  @executable_name = executable_name
  @subtools = subtools
  @separate_sources = separate_sources
  @indent = indent
  @left_column_width = left_column_width
  @wrap_width = wrap_width
  @right_column_wrap_width = wrap_width ? wrap_width - left_column_width - indent - 1 : nil
  @lines = []
  assemble
end

Private Instance Methods

add_flag(flag) click to toggle source
# File lib/toys/utils/help_text.rb, line 269
def add_flag(flag)
  flags = flag.short_flag_syntax + flag.long_flag_syntax
  last_index = flags.size - 1
  flags_str = flags.each_with_index.map do |fs, i|
    i == last_index ? fs.canonical_str : fs.str_without_value
  end.join(", ")
  flags_str = "    #{flags_str}" if flag.short_flag_syntax.empty?
  add_right_column_desc(flags_str, wrap_desc(flag.desc))
end
add_flag_group_sections() click to toggle source
# File lib/toys/utils/help_text.rb, line 256
def add_flag_group_sections
  @tool.flag_groups.each do |group|
    next if group.empty?
    @lines << ""
    desc_str = group.desc.to_s
    desc_str = "Flags" if desc_str.empty?
    @lines << "#{desc_str}:"
    group.flags.each do |flag|
      add_flag(flag)
    end
  end
end
add_positional_arguments_section() click to toggle source
# File lib/toys/utils/help_text.rb, line 279
def add_positional_arguments_section
  args_to_display = @tool.positional_args
  return if args_to_display.empty?
  @lines << ""
  @lines << "Positional arguments:"
  args_to_display.each do |arg_info|
    add_right_column_desc(arg_name(arg_info), wrap_desc(arg_info.desc))
  end
end
add_right_column_desc(initial, desc) click to toggle source
# File lib/toys/utils/help_text.rb, line 299
def add_right_column_desc(initial, desc)
  initial = indent_str(initial.ljust(@left_column_width))
  remaining_doc = desc
  if initial.size <= @indent + @left_column_width
    @lines << "#{initial} #{desc.first}"
    remaining_doc = desc[1..-1] || []
  else
    @lines << initial
  end
  remaining_doc.each do |d|
    @lines << "#{' ' * (@indent + @left_column_width)} #{d}"
  end
end
add_subtool_list_section() click to toggle source
# File lib/toys/utils/help_text.rb, line 289
def add_subtool_list_section
  @subtools.each do |source_name, subtool_list|
    @lines << ""
    @lines << (@separate_sources ? "Tools from #{source_name}:" : "Tools:")
    subtool_list.each do |local_name, subtool|
      add_right_column_desc(local_name, wrap_desc(subtool.desc))
    end
  end
end
add_synopsis_section() click to toggle source
# File lib/toys/utils/help_text.rb, line 232
def add_synopsis_section
  synopses = []
  synopses << namespace_synopsis unless @subtools.empty?
  synopses << tool_synopsis
  first = true
  synopses.each do |synopsis|
    @lines << (first ? "Usage:  #{synopsis}" : "        #{synopsis}")
    first = false
  end
end
arg_name(arg_info) click to toggle source
# File lib/toys/utils/help_text.rb, line 313
def arg_name(arg_info)
  case arg_info.type
  when :required
    arg_info.display_name
  when :optional
    "[#{arg_info.display_name}]"
  when :remaining
    "[#{arg_info.display_name}...]"
  end
end
assemble() click to toggle source
# File lib/toys/utils/help_text.rb, line 223
def assemble
  add_synopsis_section
  add_flag_group_sections
  add_positional_arguments_section if @tool.runnable?
  add_subtool_list_section
  joined_lines = @lines.join("\n")
  @result = "#{joined_lines}\n"
end
indent_str(str) click to toggle source
# File lib/toys/utils/help_text.rb, line 328
def indent_str(str)
  "#{' ' * @indent}#{str}"
end
namespace_synopsis() click to toggle source
# File lib/toys/utils/help_text.rb, line 252
def namespace_synopsis
  "#{@executable_name} #{@tool.display_name} TOOL [ARGUMENTS...]"
end
tool_synopsis() click to toggle source
# File lib/toys/utils/help_text.rb, line 243
def tool_synopsis
  synopsis = [@executable_name] + @tool.full_name
  synopsis << "[FLAGS...]" unless @tool.flags.empty?
  @tool.positional_args.each do |arg_info|
    synopsis << arg_name(arg_info)
  end
  synopsis.join(" ")
end
wrap_desc(desc) click to toggle source
# File lib/toys/utils/help_text.rb, line 324
def wrap_desc(desc)
  WrappableString.wrap_lines(desc, @right_column_wrap_width)
end