class Amazon::Coral::Option

A simple library for processing command line arguments.

Public Class Methods

new(args) click to toggle source
# File lib/amazon/coral/option.rb, line 8
def initialize(args)
  @long = args[:long]
  @short = args[:short]
  @num_parameters = args[:parameters]
  @description = args[:description]
end
parse(arguments, argv) click to toggle source

Using the provided list of arguments (defined as Option objects), parse the given argument vector.

# File lib/amazon/coral/option.rb, line 45
def Option.parse(arguments, argv)
  long_map = {}
  short_map = {}
  arguments.each { |p|
    long_map["--#{p.long}"] = p unless p.long.nil?
    short_map["-#{p.short}"] = p unless p.short.nil?
  }


  h = {}
  i = 0
  while i < argv.length
    arg = argv[i]
    a = long_map[arg]
    a = short_map[arg] if a.nil?
    raise "Unrecognized argument '#{arg}'" if a.nil?

    i = a.consume(argv, i, h)
  end

  return h
end

Public Instance Methods

consume(argv, i, hash) click to toggle source

Consume the arguments of this option from the argument vector and store them in the provided hash Returns the incremented counter of current position within the argument vector.

# File lib/amazon/coral/option.rb, line 30
def consume(argv, i, hash)
  i = i + 1
  hash[@long] = []
  unless @num_parameters.nil?
    @num_parameters.times do
      raise "Option #{@long} requires #{@num_parameters} parameter(s)" if argv.length <= i
      hash[@long] << argv[i]
      i = i + 1
    end
  end

  return i
end
description() click to toggle source

Returns a text description of this option, if present

# File lib/amazon/coral/option.rb, line 24
def description
  @description
end
long() click to toggle source

Returns the long form of this option’s name

# File lib/amazon/coral/option.rb, line 16
def long
  @long
end
short() click to toggle source

Returns the short form of this option’s name

# File lib/amazon/coral/option.rb, line 20
def short
  @short
end