From: darren kirby Date: 2008-04-02T07:28:12+09:00 Subject: Re: [PATCH 1/2] GetoptLong and ARGV against trunk Index: lib/getoptlong.rb =================================================================== --- lib/getoptlong.rb (revision 15883) +++ lib/getoptlong.rb (working copy) @@ -187,6 +187,20 @@ end # + # Operate on copy of ARGV. + # + def argslist + @argslist ||= ARGV.dup + end + + # + # Allow arbitrary arrays of options/args. + # + def argslist=(args) + @argslist = args + end + + # # Set the handling of the ordering of options and arguments. # A RuntimeError is raised if option processing has already started. # @@ -202,7 +216,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 @argslist, and only ('-a', '') being # processed as an option/arg pair. # # This is the default ordering, if the environment variable @@ -219,7 +233,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 @argslist. # # If the ordering is set to PERMUTE but the environment variable # POSIXLY_CORRECT is set, REQUIRE_ORDER is used instead. This is for @@ -358,7 +372,7 @@ @status = STATUS_TERMINATED @non_option_arguments.reverse_each do |argument| - ARGV.unshift(argument) + @argslist.unshift(argument) end @canonical_names = nil @@ -440,26 +454,26 @@ # if 0 < @rest_singles.length argument = '-' + @rest_singles - elsif (ARGV.length == 0) + elsif (@argslist.length == 0) terminate return nil elsif @ordering == PERMUTE - while 0 < ARGV.length && ARGV[0] !~ /^-./ - @non_option_arguments.push(ARGV.shift) + while 0 < @argslist.length && @argslist[0] !~ /^-./ + @non_option_arguments.push(@argslist.shift) end - if ARGV.length == 0 + if @argslist.length == 0 terminate return nil end - argument = ARGV.shift + argument = @argslist.shift elsif @ordering == REQUIRE_ORDER - if (ARGV[0] !~ /^-./) + if (@argslist[0] !~ /^-./) terminate return nil end - argument = ARGV.shift + argument = @argslist.shift else - argument = ARGV.shift + argument = @argslist.shift end # @@ -506,8 +520,8 @@ if @argument_flags[option_name] == REQUIRED_ARGUMENT if argument =~ /=(.*)$/ option_argument = $1 - elsif 0 < ARGV.length - option_argument = ARGV.shift + elsif 0 < @argslist.length + option_argument = @argslist.shift else set_error(MissingArgument, "option `#{argument}' requires an argument") @@ -515,8 +529,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 < @argslist.length && @argslist[0] !~ /^-./ + option_argument = @argslist.shift else option_argument = '' end @@ -542,8 +556,8 @@ if 0 < @rest_singles.length option_argument = @rest_singles @rest_singles = '' - elsif 0 < ARGV.length - option_argument = ARGV.shift + elsif 0 < @argslist.length + option_argument = @argslist.shift else # 1003.2 specifies the format of this message. set_error(MissingArgument, "option requires an argument -- #{ch}") @@ -552,8 +566,8 @@ if 0 < @rest_singles.length option_argument = @rest_singles @rest_singles = '' - elsif 0 < ARGV.length && ARGV[0] !~ /^-./ - option_argument = ARGV.shift + elsif 0 < @argslist.length && @argslist[0] !~ /^-./ + option_argument = @argslist.shift else option_argument = '' end