class OptParsePlus

Attributes

options[R]
summary[R]

Public Class Methods

new(parent = nil) click to toggle source
# File lib/opt_parse_plus.rb, line 19
def initialize(parent = nil)
  @parent = parent || self
  @parser = OptionParser.new
  @options = OptionsFound.new
  @commands = {}
  @summary = ""

  add_default_help
end

Public Instance Methods

add_command(command_name, &block) click to toggle source
# File lib/opt_parse_plus.rb, line 33
def add_command(command_name, &block)
  command_parser = OptParsePlus.new(self)

  base_command = base_command_name(command_name.to_s)
  rest = command_name.gsub(base_command, '')

  command_parser.banner "Usage: #$0 #{base_command} [options]#{rest}"
  command_parser.add_options(&block) if block_given?
  @commands[base_command] = command_parser
end
add_options(&block) click to toggle source
# File lib/opt_parse_plus.rb, line 29
def add_options(&block)
  instance_exec(&block)
end
banner(message) click to toggle source
description(message) click to toggle source
# File lib/opt_parse_plus.rb, line 63
def description(message)
  @summary = clean_up_white_space(message)
  @parser.separator("")
  @parser.separator(@summary)
  @parser.separator("")
end
group(group_name = nil) click to toggle source
# File lib/opt_parse_plus.rb, line 55
def group(group_name = nil)
  if group_name
    @group = group_name
  else
    @group
  end
end
help() click to toggle source
# File lib/opt_parse_plus.rb, line 93
def help
  help_parts   = []
  help_parts << @parser.to_s

  if @commands.any?
    help_parts << ["Known Commands", ""]
    command_list = group_and_sort_command_help

    command_list.each do |cmd_line|
      help_parts << cmd_line
    end

    help_parts << ""
  end

  help_parts << ""
  help_parts.flatten.join("\n")
end
option(*arguments) click to toggle source
# File lib/opt_parse_plus.rb, line 44
def option(*arguments)
  argument_name = parse_argument_name(arguments)
  @parser.on(*arguments) do |arg|
    @options[argument_name] = arg
  end
end
parse!(command_line) click to toggle source
# File lib/opt_parse_plus.rb, line 74
def parse!(command_line)
  @parser.order!(command_line)
  final = @options

  next_token = command_line.first

  if command_parser = @commands[next_token]
    command_line.shift
    command_parser.parse!(command_line)

    final.command_found = next_token
    final.merge!({
      next_token => command_parser.options
    })
  end

  final
end
set(key, value) click to toggle source
# File lib/opt_parse_plus.rb, line 70
def set(key, value)
  @options[key] = value
end

Protected Instance Methods

add_default_help() click to toggle source
# File lib/opt_parse_plus.rb, line 118
def add_default_help
  @parser.on_tail('-h', '--help', 'Show this help message') do
    puts help
    @parent.set('help', true)
  end
end
base_command_name(command_string) click to toggle source
# File lib/opt_parse_plus.rb, line 125
def base_command_name(command_string)
  command_string.split(/\s/).first
end
build_help_for_command_group(group_name, group_commands) click to toggle source
# File lib/opt_parse_plus.rb, line 150
def build_help_for_command_group(group_name, group_commands)
  command_list = []

  if group_name
    command_list << nil
    command_list << "#{group_name} commands"
    command_list << nil
  end

  command_list + generate_short_help_for_commands(group_commands)
end
clean_up_white_space(message) click to toggle source
# File lib/opt_parse_plus.rb, line 114
def clean_up_white_space(message)
  message.split("\n").map(&:strip).join("\n")
end
first_line_of_summary(parser) click to toggle source
# File lib/opt_parse_plus.rb, line 180
def first_line_of_summary(parser)
  parser.summary.split("\n").first
end
generate_short_help_for_commands(group_commands) click to toggle source
# File lib/opt_parse_plus.rb, line 162
def generate_short_help_for_commands(group_commands)
  longest_command_length = 0
  group_commands.each do |(command_name, _)|
    longest_command_length = command_name.length if command_name.length > longest_command_length
  end
  # Give ourselves a small buffer between command and summary
  longest_command_length += 3

  sorted_group_commands = group_commands.sort {|a, b| a[0] <=> b[0]}
  sorted_group_commands.map do |(command_name, parser)|
    sprintf(
      "%-#{longest_command_length}s %s",
      command_name,
      first_line_of_summary(parser)
    )
  end
end
group_and_sort_command_help() click to toggle source
# File lib/opt_parse_plus.rb, line 134
def group_and_sort_command_help
  grouped_commands = Hash.new {|hash, key| hash[key] = []}

  @commands.each do |command_name, parser|
    grouped_commands[parser.group] << [command_name, parser]
  end

  command_list = []
  sorted_group_names = grouped_commands.keys.sort {|a, b| a.to_s <=> b.to_s }
  sorted_group_names.each do |group_name|
    command_list << build_help_for_command_group(group_name, grouped_commands[group_name])
  end

  command_list.flatten(1)
end
parse_argument_name(arguments) click to toggle source
# File lib/opt_parse_plus.rb, line 129
def parse_argument_name(arguments)
  full_name_arg = arguments.select {|a| a =~ /\A--/ }.first
  full_name_arg.split(/\s/).first.gsub("--", "")
end