[前][次][番号順一覧][スレッド一覧]

ruby-changes:72740

From: konsolebox <ko1@a...>
Date: Fri, 29 Jul 2022 19:10:23 +0900 (JST)
Subject: [ruby-changes:72740] 419ad1e13e (master): [ruby/optparse] Also accept '-' as an optional argument (https://github.com/ruby/optparse/pull/35)

https://git.ruby-lang.org/ruby.git/commit/?id=419ad1e13e

From 419ad1e13e6287d0b7a6ba1dfeb485d2f889bf9e Mon Sep 17 00:00:00 2001
From: konsolebox <konsolebox@g...>
Date: Thu, 9 Jun 2022 19:43:24 +0800
Subject: [ruby/optparse] Also accept '-' as an optional argument
 (https://github.com/ruby/optparse/pull/35)

https://github.com/ruby/optparse/commit/f2b8318631
---
 lib/optparse.rb                | 6 +++---
 test/optparse/test_placearg.rb | 6 ++++++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/lib/optparse.rb b/lib/optparse.rb
index ec37bde3bd..1d42c79045 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -765,15 +765,15 @@ class OptionParser https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L765
     end
 
     #
-    # Switch that takes an argument, which does not begin with '-'.
+    # Switch that takes an argument, which does not begin with '-' or is '-'.
     #
     class PlacedArgument < self
 
       #
-      # Returns nil if argument is not present or begins with '-'.
+      # Returns nil if argument is not present or begins with '-' and is not '-'.
       #
       def parse(arg, argv, &error)
-        if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
+        if !(val = arg) and (argv.empty? or /\A-./ =~ (val = argv[0]))
           return nil, block, nil
         end
         opt = (val = parse_arg(val, &error))[1]
diff --git a/test/optparse/test_placearg.rb b/test/optparse/test_placearg.rb
index 94cfb0e819..ed0e4d3e6c 100644
--- a/test/optparse/test_placearg.rb
+++ b/test/optparse/test_placearg.rb
@@ -18,6 +18,8 @@ class TestOptionParserPlaceArg < TestOptionParser https://github.com/ruby/ruby/blob/trunk/test/optparse/test_placearg.rb#L18
   def test_short
     assert_equal(%w"", no_error {@opt.parse!(%w"-x -n")})
     assert_equal(nil, @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"-x -")})
+    assert_equal("-", @flag)
     @flag = false
     assert_equal(%w"", no_error {@opt.parse!(%w"-x foo")})
     assert_equal("foo", @flag)
@@ -30,6 +32,8 @@ class TestOptionParserPlaceArg < TestOptionParser https://github.com/ruby/ruby/blob/trunk/test/optparse/test_placearg.rb#L32
   def test_abbrev
     assert_equal(%w"", no_error {@opt.parse!(%w"-o -n")})
     assert_equal(nil, @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"-o -")})
+    assert_equal("-", @flag)
     @flag = false
     assert_equal(%w"", no_error {@opt.parse!(%w"-o foo")})
     assert_equal("foo", @flag)
@@ -42,6 +46,8 @@ class TestOptionParserPlaceArg < TestOptionParser https://github.com/ruby/ruby/blob/trunk/test/optparse/test_placearg.rb#L46
   def test_long
     assert_equal(%w"", no_error {@opt.parse!(%w"--opt -n")})
     assert_equal(nil, @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"--opt -")})
+    assert_equal("-", @flag)
     assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt= foo")})
     assert_equal("", @flag)
     assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")})
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]