module MyUtilities

Constants

VERSION

Attributes

help_string[RW]

Public Class Methods

error_exit(msg) click to toggle source
# File lib/my_utilities.rb, line 21
def self.error_exit(msg)
  puts "ERROR: #{msg}"
  exit -1
end
print_help_and_exit() click to toggle source
process_cli(opt_map) click to toggle source
# 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

Private Class Methods

generate_option_strings() click to toggle source
# File lib/my_utilities.rb, line 110
def self.generate_option_strings
  a=generate_single_option
  return [a, "#{a}_long_option"]
end
generate_single_option() click to toggle source
# File lib/my_utilities.rb, line 115
def self.generate_single_option
  # This will fail terribly after 62 incorrectly specified options
  ([a..z]+[A..Z]+[0..9]).each do |opt|
    unless @used_single_options.include? opt
      @used_single_options << opt
      return opt
    end
  end

  return 'a'
end