Class: Amazon::Coral::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/coral/option.rb

Overview

A simple library for processing command line arguments.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Option

Returns a new instance of Option.



8
9
10
11
12
13
# 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

Class Method Details

.parse(arguments, argv) ⇒ Object

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

Instance Method Details

#consume(argv, i, hash) ⇒ Object

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.



30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

#descriptionObject

Returns a text description of this option, if present



24
25
26
# File 'lib/amazon/coral/option.rb', line 24

def description
  @description
end

#longObject

Returns the long form of this option’s name



16
17
18
# File 'lib/amazon/coral/option.rb', line 16

def long
  @long
end

#shortObject

Returns the short form of this option’s name



20
21
22
# File 'lib/amazon/coral/option.rb', line 20

def short
  @short
end