Module: MyUtilities

Defined in:
lib/my_utilities.rb,
lib/my_utilities/version.rb

Defined Under Namespace

Modules: MyDir Classes: Logger

Constant Summary collapse

VERSION =
"2.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.help_stringObject

Returns the value of attribute help_string.



9
10
11
# File 'lib/my_utilities.rb', line 9

def help_string
  @help_string
end

Class Method Details

.error_exit(msg) ⇒ Object



21
22
23
24
# File 'lib/my_utilities.rb', line 21

def self.error_exit(msg)
  puts "ERROR: #{msg}"
  exit -1
end


12
13
14
15
16
17
18
19
# File 'lib/my_utilities.rb', line 12

def self.print_help_and_exit
  puts <<EOS
#{__FILE__} [options]
#{@help_string}
EOS

  exit -1
end

.process_cli(opt_map) ⇒ Object



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
# File 'lib/my_utilities.rb', line 26

def self.process_cli(opt_map)
  return [] if Hash != opt_map.class or opt_map.empty?
  return_array = []

  opt_array = []
  key_of = {}
  opt_map.each do |k, v|
    # If one of the keys is not a symbol, let's abort.
    return [] unless k.is_a? Symbol


    matches = /^\-(.)(:\-\-(.*)?)/.match v
    single_opt = long_opt = nil

    # We will generate options regardless of whether the input string is valid.
    if !matches.nil?
      single_opt = matches[1]
      long_opt = matches[3]
    end

    gen_opt1, gen_opt2 = generate_option_strings
    single_opt ||= gen_opt1
    long_opt ||= gen_opt2
    key_of["--#{long_opt}"]=k

    opt_array << ["--#{long_opt}," "-#{single_opt}", GetoptLong::OPTIONAL_ARGUMENT]
  end

  opts = GetoptLong.new opt_array

  opts.each do |opt, arg|
    return_array[key_of[opt]]=arg
  end

  return_array
end