Class: Af::OptionParser::GetOptions

Inherits:
GetoptLong
  • Object
show all
Defined in:
lib/fiksu-af/option_parser/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(declared_options = []) ⇒ 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>


25
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
# File 'lib/fiksu-af/option_parser/get_options.rb', line 25

def initialize(declared_options = [])
  getopt_options = []
  argv_additions = []

  # Iterate through all of the options.
  declared_options.each do |option|
    # Set aside
    if option.environment_variable.present?
      # Add enviroment variables to the front of ARGV.
      if ENV[option.environment_variable]
        argv_additions << option.long_name
        unless ENV[option.environment_variable].empty?
          # if the envvar is empty we assume this is a switch (no parameter)
          argv_additions << ENV[option.environment_variable]
        end
      end
    end

    # Convert hash into array, in format expected by Getoptlong#new.
    # Example: ['--foo', '-f', GetoptLong::NO_ARGUMENT]
    options = []
    options << option.long_name
    if (option.short_name)
      options << option.short_name
    end
    options << option.requirements
    getopt_options << options
  end

  # add any ARGVs to our list
  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