class Fig::Command::Options::Parser

Command-line processing.

Constants

FULL_USAGE
SHORT_USAGE

This class knows way too much about how OptionParser works.

Public Class Methods

new() click to toggle source
# File lib/fig/command/options/parser.rb, line 126
def initialize()
  @switches             = {}
  @argument_description = {}
  @parser               = OptionParser.new

  @parser.banner = "#{FULL_USAGE}\nAll options:\n\n"
end

Public Instance Methods

add_argument_description(options, description) click to toggle source
# File lib/fig/command/options/parser.rb, line 134
def add_argument_description(options, description)
  if options.is_a? Array
    options.each do
      |option|

      @argument_description[option] = description
    end
  else
    @argument_description[options] = description
  end

  return
end
full_help() click to toggle source
# File lib/fig/command/options/parser.rb, line 188
def full_help()
  return @parser.help
end
on(*arguments, &block) click to toggle source
# File lib/fig/command/options/parser.rb, line 158
def on(*arguments, &block)
  switch_array = make_switch_array(arguments, block)

  return if not switch_array

  @parser.top.append(*switch_array)

  return
end
on_head(*arguments, &block) click to toggle source
# File lib/fig/command/options/parser.rb, line 148
def on_head(*arguments, &block)
  switch_array = make_switch_array(arguments, block)

  return if not switch_array

  @parser.top.prepend(*switch_array)

  return
end
on_tail(*arguments, &block) click to toggle source
# File lib/fig/command/options/parser.rb, line 174
def on_tail(*arguments, &block)
  switch_array = make_switch_array(arguments, block)

  return if not switch_array

  @parser.base.append(*switch_array)

  return
end
options_message() click to toggle source
# File lib/fig/command/options/parser.rb, line 192
def options_message()
  return @parser.summarize('')
end
parse!(argv) click to toggle source
# File lib/fig/command/options/parser.rb, line 196
def parse!(argv)
  begin
    @parser.parse!(argv)
  rescue OptionParser::InvalidArgument => error
    raise_invalid_argument(error.args[0], error.args[1])
  rescue OptionParser::MissingArgument => error
    raise_missing_argument(error.args[0])
  rescue OptionParser::InvalidOption => error
    raise Fig::Command::OptionError.new(
      "Unknown option #{error.args[0]}.\n\n#{SHORT_USAGE}"
    )
  rescue OptionParser::ParseError => error
    raise Fig::Command::OptionError.new(error.to_s)
  end

  return
end
raise_invalid_argument(option, value, description = nil) click to toggle source
# File lib/fig/command/options/parser.rb, line 214
def raise_invalid_argument(option, value, description = nil)
  # *sigh* OptionParser does not raise MissingArgument for the case of an
  # option with a required value being followed by another option.  It
  # assigns the next option as the value instead.  E.g. for
  #
  #    fig --set --get FOO
  #
  # it assigns "--get" as the value of the "--set" option.
  if @switches.has_key? value
    raise_missing_argument(option)
  end

  description ||= @argument_description[option]
  if description.nil?
    description = ''
  else
    description = ' ' + description
  end

  raise Fig::Command::OptionError.new(
    %Q<Invalid value for #{option}: "#{value}"#{description}>
  )
end
separator(string) click to toggle source
# File lib/fig/command/options/parser.rb, line 168
def separator(string)
  @parser.separator string

  return
end
short_help() click to toggle source
# File lib/fig/command/options/parser.rb, line 184
def short_help()
  return SHORT_USAGE
end

Private Instance Methods

make_switch_array(arguments, block) click to toggle source
# File lib/fig/command/options/parser.rb, line 240
def make_switch_array(arguments, block)
  # This method is a means of interjecting ourselves between the creation of
  # a Switch object and putting it into the list of actual switches.
  #
  # From the OptionParser code, the contents of the array:
  #
  # +switch+::      OptionParser::Switch instance to be inserted.
  # +short_opts+::  List of short style options.
  # +long_opts+::   List of long style options.
  # +nolong_opts+:: List of long style options with "no-" prefix.
  #
  # Why returning this data separate from the Switch object is necessary, I
  # do not understand.

  switch_array = @parser.make_switch(arguments, block)
  switch = switch_array[0]

  options = [switch.long, switch.short].flatten

  return if options.any? {|option| @switches.has_key? option}

  options.each {|option| @switches[option] = switch}

  return switch_array
end
raise_missing_argument(option) click to toggle source
# File lib/fig/command/options/parser.rb, line 266
def raise_missing_argument(option)
  raise Fig::Command::OptionError.new(
    "Please provide a value for #{option}."
  )
end