lib/getopt/long.rb in getopt-1.3.5 vs lib/getopt/long.rb in getopt-1.3.6

- old
+ new

@@ -5,16 +5,34 @@ OPTIONAL = 2 INCREMENT = 3 NEGATABLE = 4 NUMERIC = 5 - class LongError < StandardError; end - class Long - VERSION = '1.3.5' + class Error < StandardError; end - # Takes an array of switches. Each array consists of three elements. + VERSION = '1.3.6' + + # Takes an array of switches. Each array consists of up to three + # elements that indicate the name and type of switch. Returns a hash + # containing each switch name, minus the '-', as a key. The value + # for each key depends on the type of switch and/or the value provided + # by the user. + # + # The long switch _must_ be provided. The short switch defaults to the + # first letter of the short switch. The default type is BOOLEAN. + # + # Example: + # + # opts = Getopt::Long.getopts( + # ["--debug"], + # ["--verbose", "-v"], + # ["--level", "-l", NUMERIC] + # ) + # + # See the README file for more information. + # def self.getopts(*switches) if switches.empty? raise ArgumentError, "no switches provided" end @@ -67,11 +85,11 @@ if re_short_sq.match(opt) chars = opt.split("")[1..-1].map{ |s| s = "-#{s}" } chars.each_with_index{ |char, i| unless valid.include?(char) - raise LongError, "invalid switch '#{char}'" + raise Error, "invalid switch '#{char}'" end # Grab the next arg if the switch takes a required arg if types[char] == REQUIRED # Deal with a argument squished up against switch @@ -81,11 +99,11 @@ break else arg = ARGV.delete_at(index+1) if arg.nil? || valid.include?(arg) # Minor cheat here err = "no value provided for required argument '#{char}'" - raise LongError, err + raise Error, err end ARGV.push(char, arg) end elsif types[char] == OPTIONAL if chars[i+1] && !valid.include?(chars[i+1]) @@ -120,27 +138,27 @@ # Make sure that all the switches are valid. If 'switch' isn't # defined at this point, it means an option was passed without # a preceding switch, e.g. --option foo bar. unless valid.include?(switch) switch ||= opt - raise LongError, "invalid switch '#{switch}'" + raise Error, "invalid switch '#{switch}'" end # Required arguments if types[switch] == REQUIRED nextval = ARGV[index+1] # Make sure there's a value for mandatory arguments if nextval.nil? err = "no value provided for required argument '#{switch}'" - raise LongError, err + raise Error, err end # If there is a value, make sure it's not another switch if valid.include?(nextval) err = "cannot pass switch '#{nextval}' as an argument" - raise LongError, err + raise Error, err end # If the same option appears more than once, put the values # in array. if hash[switch] @@ -152,10 +170,10 @@ end # For boolean arguments set the switch's value to true. if types[switch] == BOOLEAN if hash.has_key?(switch) - raise LongError, "boolean switch already set" + raise Error, "boolean switch already set" end hash[switch] = true end # For increment arguments, set the switch's value to 0, or