class Thunder::DefaultHelp

Provides an easy to parse help formatter

Public Class Methods

help_command(command_spec) click to toggle source

@see Thunder#help_command(command_spec)

# File lib/thunder/help/default.rb, line 6
    def help_command(command_spec)
      preamble = determine_preamble
      footer = ""
      footer << command_spec[:description] + "\n" if command_spec[:description]
      footer << command_spec[:long_description] + "\n" if command_spec[:long_description]
      footer << "\n" + format_options(command_spec[:options]) if command_spec[:options]
      output = <<-EOS
Usage:
  #{preamble} #{command_spec[:usage]}

#{footer.strip}
      EOS
      output.rstrip
    end
help_list(commands) click to toggle source

@see Thunder#help_list(commands)

# File lib/thunder/help/default.rb, line 22
def help_list(commands)
  preamble = determine_preamble
  help = []
  commands.each do |name, command_spec|
    help << short_help(preamble, command_spec)
  end
  render_table(help)
end

Private Class Methods

determine_preamble() click to toggle source

determine the preamble

@return [String] the preamble

# File lib/thunder/help/default.rb, line 58
def determine_preamble
  preamble = "#{File.basename($0)}"
  ARGV.each do |arg|
    break if arg == "help"
    preamble << " #{arg}"
  end
  preamble
end
format_option(option_spec) click to toggle source

format an option

@param option_spec [Hash] the option spec to format @return [(String, String)] the formatted option and its description

# File lib/thunder/help/default.rb, line 49
def format_option(option_spec)
  usage = "  -#{option_spec[:short]}, --#{option_spec[:name]}"
  usage << " [#{option_spec[:name].to_s.upcase}]" unless option_spec[:type] == Thunder::Boolean
  return usage, option_spec[:desc]
end
format_options(options) click to toggle source

format a set of option specs

@param options [<Hash>] the option specs to format @return [String]

# File lib/thunder/help/default.rb, line 37
def format_options(options)
  data = []
  options.each do |name, option_spec|
    data << format_option(option_spec)
  end
  "Options:\n" + render_table(data, ": ")
end
render_table(data, separator = " click to toggle source

render a two-column table

@param data [(String,String)] @param separator [String] @return [String] a two-column table

# File lib/thunder/help/default.rb, line 81
def render_table(data, separator = " # ")
  column_width = data.group_by do |row|
    row.first.size
  end.max.first
  "".tap do |output|
    data.each do |row|
      output << "%-#{column_width}s#{separator}%s\n" % row
    end
  end
end
short_help(preamble, command_spec) click to toggle source

render the short help string for a command

@param preamble [String] the preamble @param command_spec [Hash] @return [String] the short help string for the given command

# File lib/thunder/help/default.rb, line 72
def short_help(preamble, command_spec)
  return "  #{preamble} #{command_spec[:usage]}", command_spec[:description]
end