Class: Af::GetOptions

Inherits:
GetoptLong
  • Object
show all
Defined in:
lib/fiksu-af/get_options.rb

Overview

Constant Summary collapse

ARGUMENT_FLAGS =

Local constants which map to superclass argument types.

[
  NO_ARGUMENT = GetoptLong::NO_ARGUMENT,
  REQUIRED_ARGUMENT = GetoptLong::REQUIRED_ARGUMENT,
  OPTIONAL_ARGUMENT = GetoptLong::OPTIONAL_ARGUMENT
]

Instance Method Summary collapse

Constructor Details

#initialize(switches = {}) ⇒ GetOptions

Instantiate a new long command line option parser with a hash of switches.

Arguments

* switches - optional hash of command line switches, with long switch as
    key to a set of options:
      :short => <optional short switch>
      :argument => <constant arg type>
      :environment_variable => <how do these work???>
      :note => <arg description>


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fiksu-af/get_options.rb', line 26

def initialize(switches = {})
  environment_variables = {} # switches that are set in the ENV
  getopt_options = []

  # Iterate through all of the switches.
  switches.each do |long_switch, parameters|

    # Set aside 
    if parameters[:environment_variable].present?
      environment_variables[parameters[:environment_variable]] = long_switch
    end

    # Convert hash into array, in format expected by Getoptlong#new.
    # Example: ['--foo', '-f', 'bar']
    options = []
    options << long_switch
    if (parameters[:short])
      options << parameters[:short]
    end
    options << parameters[:argument]
    getopt_options << options
  end

  # Add enviroment variables to the front of ARGV.
  argv_additions = []
  for environment_variable_name,value in environment_variables do
    if ENV[environment_variable_name]
    argv_additions << value
    argv_additions << ENV[environment_variable_name] unless ENV[environment_variable_name].empty?
    end
  end
  for arg in ARGV do
    argv_additions << arg
  end

  # Rewrite ARGV with environment variable with the new list.
  argv_additions.each_with_index { |v,i| ARGV[i] = v }

  super(*getopt_options)
end