--- getoptlong.rb	2007-06-23 20:39:41.000000000 +0000
+++ getoptlong.new.rb	2007-06-23 20:41:39.000000000 +0000
@@ -90,6 +90,24 @@
 #
 #     hello -n 6 --name -- /tmp
 #
+# GetoptLong will parse ARGV by default but any array can be parsed
+# instead by simply setting the <tt>arglist</tt> attribute before
+# parsing, that is calling, <tt>#each</tt> or <tt>#get</tt>.
+#
+# Use dup to reuse the same GetoptLong object as in the following
+# example:
+#
+#   opt = GetoptLong.new([ '-x', GetoptLong::NO_ARGUMENT ],[ '-b',
+#   GetoptLong::REQUIRED_ARGUMENT])
+#   
+#   blk = lambda do
+#    |*v|
+#    puts v.join(": ")
+#   end
+#   opt.dup.each %w{ -x -b 1024}, &blk
+#   
+#   opt.dup.each %w{ -b 4096 }, &blk
+
 class GetoptLong
   #
   # Orderings.
@@ -116,6 +134,7 @@
   class MissingArgument  < Error; end
   class InvalidOption    < Error; end
 
+  attribute_accessor :arglist
   #
   # Set up option processing.
   #
@@ -133,6 +152,10 @@
   # Other than that, the elements of each sub-array can be in any order.
   #
   def initialize(*arguments)
+	#
+    # Define default for arglist
+    #
+	@arglist = ARGV
     #
     # Current ordering.
     #
@@ -183,7 +206,7 @@
 
     #
     # List of non-option-arguments.
-    # Append them to ARGV when option processing is terminated.
+    # Append them to @arglist when option processing is terminated.
     #
     @non_option_arguments = Array.new
 
@@ -208,7 +231,7 @@
   #
   # For example, if -a and -b are options which do not take arguments,
   # parsing command line arguments of '-a one -b two' would result in 
-  # 'one', '-b', 'two' being left in ARGV, and only ('-a', '') being 
+  # 'one', '-b', 'two' being left in @arglist, and only ('-a', '') being 
   # processed as an option/arg pair.
   #
   # This is the default ordering, if the environment variable
@@ -225,7 +248,7 @@
   # For example, if -a does not require an argument and -b optionally takes
   # an argument, parsing '-a one -b two three' would result in ('-a','') and
   # ('-b', 'two') being processed as option/arg pairs, and 'one','three'
-  # being left in ARGV.
+  # being left in @arglist.
   #
   # If the ordering is set to PERMUTE but the environment variable
   # POSIXLY_CORRECT is set, REQUIRE_ORDER is used instead. This is for
@@ -368,9 +391,7 @@
     raise RuntimeError, "an error has occured" if @error != nil
 
     @status = STATUS_TERMINATED
-    @non_option_arguments.reverse_each do |argument|
-      ARGV.unshift(argument)
-    end
+	@arglist.unshift( *@non_option_arguments )
 
     @canonical_names = nil
     @argument_flags = nil
@@ -451,26 +472,26 @@
     #
     if 0 < @rest_singles.length
       argument = '-' + @rest_singles
-    elsif (ARGV.length == 0)
+    elsif (@arglist.length == 0)
       terminate
       return nil
     elsif @ordering == PERMUTE
-      while 0 < ARGV.length && ARGV[0] !~ /^-./
-	@non_option_arguments.push(ARGV.shift)
+      while 0 < @arglist.length && @arglist[0] !~ /^-./
+	@non_option_arguments.push(@arglist.shift)
       end
-      if ARGV.length == 0
+      if @arglist.length == 0
 	terminate
 	return nil
       end
-      argument = ARGV.shift
+      argument = @arglist.shift
     elsif @ordering == REQUIRE_ORDER 
-      if (ARGV[0] !~ /^-./)
+      if (@arglist[0] !~ /^-./)
 	terminate
 	return nil
       end
-      argument = ARGV.shift
+      argument = @arglist.shift
     else
-      argument = ARGV.shift
+      argument = @arglist.shift
     end
 
     #
@@ -517,8 +538,8 @@
       if @argument_flags[option_name] == REQUIRED_ARGUMENT
 	if argument =~ /=(.*)$/
 	  option_argument = $1
-	elsif 0 < ARGV.length
-	  option_argument = ARGV.shift
+	elsif 0 < @arglist.length
+	  option_argument = @arglist.shift
 	else
 	  set_error(MissingArgument,
 	            "option `#{argument}' requires an argument")
@@ -526,8 +547,8 @@
       elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
 	if argument =~ /=(.*)$/
 	  option_argument = $1
-	elsif 0 < ARGV.length && ARGV[0] !~ /^-./
-	  option_argument = ARGV.shift
+	elsif 0 < @arglist.length && @arglist[0] !~ /^-./
+	  option_argument = @arglist.shift
 	else
 	  option_argument = ''
 	end
@@ -553,8 +574,8 @@
 	  if 0 < @rest_singles.length
 	    option_argument = @rest_singles
 	    @rest_singles = ''
-	  elsif 0 < ARGV.length
-	    option_argument = ARGV.shift
+	  elsif 0 < @arglist.length
+	    option_argument = @arglist.shift
 	  else
 	    # 1003.2 specifies the format of this message.
 	    set_error(MissingArgument, "option requires an argument -- #{ch}")
@@ -563,8 +584,8 @@
 	  if 0 < @rest_singles.length
 	    option_argument = @rest_singles
 	    @rest_singles = ''
-	  elsif 0 < ARGV.length && ARGV[0] !~ /^-./
-	    option_argument = ARGV.shift
+	  elsif 0 < @arglist.length && @arglist[0] !~ /^-./
+	    option_argument = @arglist.shift
 	  else
 	    option_argument = ''
 	  end
