ruby-changes:47606
From: nobu <ko1@a...>
Date: Sat, 2 Sep 2017 11:05:39 +0900 (JST)
Subject: [ruby-changes:47606] nobu:r59722 (trunk): getoptlong.rb: multiline regexps
nobu 2017-09-02 11:05:34 +0900 (Sat, 02 Sep 2017) New Revision: 59722 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59722 Log: getoptlong.rb: multiline regexps * lib/getoptlong.rb: make regexps multiline safe. [ruby-core:82627] [Bug #13858] Modified files: trunk/lib/getoptlong.rb trunk/spec/rubyspec/library/getoptlong/shared/get.rb Index: lib/getoptlong.rb =================================================================== --- lib/getoptlong.rb (revision 59721) +++ lib/getoptlong.rb (revision 59722) @@ -310,7 +310,7 @@ class GetoptLong https://github.com/ruby/ruby/blob/trunk/lib/getoptlong.rb#L310 # next if i == argument_flag begin - if !i.is_a?(String) || i !~ /^-([^-]|-.+)$/ + if !i.is_a?(String) || i !~ /\A-([^-]|-.+)\z/ raise ArgumentError, "an invalid option `#{i}'" end if (@canonical_names.include?(i)) @@ -447,7 +447,7 @@ class GetoptLong https://github.com/ruby/ruby/blob/trunk/lib/getoptlong.rb#L447 terminate return nil elsif @ordering == PERMUTE - while 0 < ARGV.length && ARGV[0] !~ /^-./ + while 0 < ARGV.length && ARGV[0] !~ /\A-./ @non_option_arguments.push(ARGV.shift) end if ARGV.length == 0 @@ -456,7 +456,7 @@ class GetoptLong https://github.com/ruby/ruby/blob/trunk/lib/getoptlong.rb#L456 end argument = ARGV.shift elsif @ordering == REQUIRE_ORDER - if (ARGV[0] !~ /^-./) + if (ARGV[0] !~ /\A-./) terminate return nil end @@ -477,7 +477,7 @@ class GetoptLong https://github.com/ruby/ruby/blob/trunk/lib/getoptlong.rb#L477 # # Check for long and short options. # - if argument =~ /^(--[^=]+)/ && @rest_singles.length == 0 + if argument =~ /\A(--[^=]+)/ && @rest_singles.length == 0 # # This is a long style option, which start with `--'. # @@ -507,7 +507,7 @@ class GetoptLong https://github.com/ruby/ruby/blob/trunk/lib/getoptlong.rb#L507 # Check an argument to the option. # if @argument_flags[option_name] == REQUIRED_ARGUMENT - if argument =~ /=(.*)$/ + if argument =~ /=(.*)/m option_argument = $1 elsif 0 < ARGV.length option_argument = ARGV.shift @@ -516,19 +516,19 @@ class GetoptLong https://github.com/ruby/ruby/blob/trunk/lib/getoptlong.rb#L516 "option `#{argument}' requires an argument") end elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT - if argument =~ /=(.*)$/ + if argument =~ /=(.*)/m option_argument = $1 - elsif 0 < ARGV.length && ARGV[0] !~ /^-./ + elsif 0 < ARGV.length && ARGV[0] !~ /\A-./ option_argument = ARGV.shift else option_argument = '' end - elsif argument =~ /=(.*)$/ + elsif argument =~ /=(.*)/m set_error(NeedlessArgument, "option `#{option_name}' doesn't allow an argument") end - elsif argument =~ /^(-(.))(.*)/ + elsif argument =~ /\A(-(.))(.*)/m # # This is a short style option, which start with `-' (not `--'). # Short options may be catenated (e.g. `-l -g' is equivalent to @@ -555,7 +555,7 @@ class GetoptLong https://github.com/ruby/ruby/blob/trunk/lib/getoptlong.rb#L555 if 0 < @rest_singles.length option_argument = @rest_singles @rest_singles = '' - elsif 0 < ARGV.length && ARGV[0] !~ /^-./ + elsif 0 < ARGV.length && ARGV[0] !~ /\A-./ option_argument = ARGV.shift else option_argument = '' Index: spec/rubyspec/library/getoptlong/shared/get.rb =================================================================== --- spec/rubyspec/library/getoptlong/shared/get.rb (revision 59721) +++ spec/rubyspec/library/getoptlong/shared/get.rb (revision 59722) @@ -52,4 +52,10 @@ describe :getoptlong_get, shared: true d https://github.com/ruby/ruby/blob/trunk/spec/rubyspec/library/getoptlong/shared/get.rb#L52 lambda { @opts.send(@method) }.should raise_error(GetoptLong::MissingArgument) end end + + it "returns multiline argument" do + argv [ "--size=\n10k\n" ] do + @opts.send(@method).should == [ "--size", "\n10k\n" ] + end + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/