lib/getopt/long.rb in getopt-1.3.1 vs lib/getopt/long.rb in getopt-1.3.2
- old
+ new
@@ -7,23 +7,23 @@
NEGATABLE = 4
class LongError < StandardError; end
class Long
- VERSION = "1.3.1"
+ VERSION = "1.3.2"
- # takes an array of switches. Each array consists of three elements.
+ # Takes an array of switches. Each array consists of three elements.
def self.getopts(*switches)
+ if switches.empty?
+ raise ArgumentError, "no switches provided"
+ end
+
hash = {} # Hash returned to user
valid = [] # Tracks valid switches
types = {} # Tracks argument types
syns = {} # Tracks long and short arguments, or multiple shorts
- if switches.empty?
- raise ArgumentError, "no switches provided"
- end
-
# If a string is passed, split it and convert it to an array of arrays
if switches.first.kind_of?(String)
switches = switches.join.split
switches.map!{ |switch| switch = [switch] }
end
@@ -54,11 +54,11 @@
re_long_eq = /^(--\w+?)=(.*?)$|^(-\w?)=(.*?)$/
re_short_sq = /^(-\w)(\S+?)$/
ARGV.each_with_index{ |opt, index|
- # Allow either -x -v or -xv style for sing char args
+ # Allow either -x -v or -xv style for single char args
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)
@@ -108,11 +108,14 @@
switch, value = match.captures.compact
ARGV.push(switch, value)
next
end
- # Make sure that all the switches are valid
+ # 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}'"
end
# Required arguments
if types[switch] == REQUIRED