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

ruby-changes:44346

From: nobu <ko1@a...>
Date: Fri, 14 Oct 2016 17:20:38 +0900 (JST)
Subject: [ruby-changes:44346] nobu:r56419 (trunk): optparse.rb: hyphenize

nobu	2016-10-14 17:20:26 +0900 (Fri, 14 Oct 2016)

  New Revision: 56419

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56419

  Log:
    optparse.rb: hyphenize
    
    * lib/optparse.rb (make_switch, parse_in_order): unify underscores
      to hyphens.

  Modified files:
    trunk/ChangeLog
    trunk/lib/optparse.rb
    trunk/test/optparse/test_noarg.rb
    trunk/test/optparse/test_optarg.rb
    trunk/test/optparse/test_placearg.rb
    trunk/test/optparse/test_reqarg.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56418)
+++ ChangeLog	(revision 56419)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Oct 14 17:20:24 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/optparse.rb (make_switch, parse_in_order): unify underscores
+	  to hyphens.
+
 Fri Oct 14 10:48:37 2016  Eric Wong  <e@8...>
 
 	* lib/webrick/utils.rb (TimeoutHandler): use monotonic clock
Index: lib/optparse.rb
===================================================================
--- lib/optparse.rb	(revision 56418)
+++ lib/optparse.rb	(revision 56419)
@@ -1386,7 +1386,8 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1386
         default_style = Switch::NoArgument
         default_pattern, conv = search(:atype, FalseClass) unless default_pattern
         ldesc << "--no-#{q}"
-        long << 'no-' + (q = q.downcase)
+        (q = q.downcase).tr!('_', '-')
+        long << "no-#{q}"
         nolong << q
       when /^--\[no-\]([^\[\]=\s]*)(.+)?/
         q, a = $1, $2
@@ -1396,10 +1397,11 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1397
           default_pattern, conv = search(:atype, o) unless default_pattern
         end
         ldesc << "--[no-]#{q}"
-        long << (o = q.downcase)
+        (o = q.downcase).tr!('_', '-')
+        long << o
         not_pattern, not_conv = search(:atype, FalseClass) unless not_style
         not_style = Switch::NoArgument
-        nolong << 'no-' + o
+        nolong << "no-#{o}"
       when /^--([^\[\]=\s]*)(.+)?/
         q, a = $1, $2
         if a
@@ -1408,7 +1410,8 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1410
           default_pattern, conv = search(:atype, o) unless default_pattern
         end
         ldesc << "--#{q}"
-        long << (o = q.downcase)
+        (o = q.downcase).tr!('_', '-')
+        long << o
       when /^-(\[\^?\]?(?:[^\\\]]|\\.)*\])(.+)?/
         q, a = $1, $2
         o = notwice(Object, klass, 'type')
@@ -1538,6 +1541,7 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1541
         # long option
         when /\A--([^=]*)(?:=(.*))?/m
           opt, rest = $1, $2
+          opt.tr!('_', '-')
           begin
             sw, = complete(:long, opt, true)
           rescue ParseError
Index: test/optparse/test_optarg.rb
===================================================================
--- test/optparse/test_optarg.rb	(revision 56418)
+++ test/optparse/test_optarg.rb	(revision 56419)
@@ -7,6 +7,8 @@ class TestOptionParser::OptArg < TestOpt https://github.com/ruby/ruby/blob/trunk/test/optparse/test_optarg.rb#L7
     @opt.def_option("-x[VAL]") {|x| @flag = x}
     @opt.def_option("--option[=VAL]") {|x| @flag = x}
     @opt.def_option("--regexp[=REGEXP]", Regexp) {|x| @reopt = x}
+    @opt.def_option "--with_underscore[=VAL]" do |x| @flag = x end
+    @opt.def_option "--with-hyphen[=VAL]" do |x| @flag = x end
     @reopt = nil
   end
 
@@ -44,4 +46,15 @@ class TestOptionParser::OptArg < TestOpt https://github.com/ruby/ruby/blob/trunk/test/optparse/test_optarg.rb#L46
     assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt foo")})
     assert_equal(nil, @flag)
   end
+
+  def test_hyphenize
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore=foo1")})
+    assert_equal("foo1", @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore=foo2")})
+    assert_equal("foo2", @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen=foo3")})
+    assert_equal("foo3", @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")})
+    assert_equal("foo4", @flag)
+  end
 end
Index: test/optparse/test_placearg.rb
===================================================================
--- test/optparse/test_placearg.rb	(revision 56418)
+++ test/optparse/test_placearg.rb	(revision 56419)
@@ -11,6 +11,8 @@ class TestOptionParser::PlaceArg < TestO https://github.com/ruby/ruby/blob/trunk/test/optparse/test_placearg.rb#L11
     @opt.def_option("-n") {}
     @opt.def_option("--regexp [REGEXP]", Regexp) {|x| @reopt = x}
     @reopt = nil
+    @opt.def_option "--with_underscore=VAL" do |x| @flag = x end
+    @opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
   end
 
   def test_short
@@ -48,6 +50,17 @@ class TestOptionParser::PlaceArg < TestO https://github.com/ruby/ruby/blob/trunk/test/optparse/test_placearg.rb#L50
     assert_equal("bar", @flag)
   end
 
+  def test_hyphenize
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore=foo1")})
+    assert_equal("foo1", @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore=foo2")})
+    assert_equal("foo2", @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen=foo3")})
+    assert_equal("foo3", @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")})
+    assert_equal("foo4", @flag)
+  end
+
   def test_conv
     assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T te.rb")})
     assert_nil(@topt)
Index: test/optparse/test_reqarg.rb
===================================================================
--- test/optparse/test_reqarg.rb	(revision 56418)
+++ test/optparse/test_reqarg.rb	(revision 56419)
@@ -2,6 +2,12 @@ https://github.com/ruby/ruby/blob/trunk/test/optparse/test_reqarg.rb#L2
 require_relative 'test_optparse'
 
 module TestOptionParser::ReqArg
+  def setup
+    super
+    @opt.def_option "--with_underscore=VAL" do |x| @flag = x end
+    @opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
+  end
+
   class Def1 < TestOptionParser
     include ReqArg
     def setup
@@ -64,6 +70,17 @@ module TestOptionParser::ReqArg https://github.com/ruby/ruby/blob/trunk/test/optparse/test_reqarg.rb#L70
     assert_equal("foo", @flag)
   end
 
+  def test_hyphenize
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore foo1")})
+    assert_equal("foo1", @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore foo2")})
+    assert_equal("foo2", @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen foo3")})
+    assert_equal("foo3", @flag)
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen foo4")})
+    assert_equal("foo4", @flag)
+  end
+
   class TestOptionParser::WithPattern < TestOptionParser
     def test_pattern
       pat = num = nil
Index: test/optparse/test_noarg.rb
===================================================================
--- test/optparse/test_noarg.rb	(revision 56418)
+++ test/optparse/test_noarg.rb	(revision 56419)
@@ -2,6 +2,12 @@ https://github.com/ruby/ruby/blob/trunk/test/optparse/test_noarg.rb#L2
 require_relative 'test_optparse'
 
 module TestOptionParser::NoArg
+  def setup
+    super
+    @opt.def_option "--with_underscore" do |x| @flag = x end
+    @opt.def_option "--with-hyphen" do |x| @flag = x end
+  end
+
   class Def1 < TestOptionParser
     include NoArg
     def setup
@@ -55,4 +61,19 @@ module TestOptionParser::NoArg https://github.com/ruby/ruby/blob/trunk/test/optparse/test_noarg.rb#L61
     assert_equal(%w"", no_error {@opt.parse!(%w"--opt")})
     assert_equal(true, @flag)
   end
+
+  def test_hyphenize
+    @flag = nil
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore")})
+    assert_equal(true, @flag)
+    @flag = nil
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore")})
+    assert_equal(true, @flag)
+    @flag = nil
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen")})
+    assert_equal(true, @flag)
+    @flag = nil
+    assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen")})
+    assert_equal(true, @flag)
+  end
 end

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

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