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

ruby-changes:43154

From: nobu <ko1@a...>
Date: Tue, 31 May 2016 17:28:52 +0900 (JST)
Subject: [ruby-changes:43154] nobu:r55228 (trunk): optparse.rb: fix char class option

nobu	2016-05-31 17:28:48 +0900 (Tue, 31 May 2016)

  New Revision: 55228

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

  Log:
    optparse.rb: fix char class option
    
    * lib/optparse.rb (OptionParser::Completion.candidate): get rid of
      nil as key names.  [ruby-core:75773] [Bug #12438]
    * lib/optparse.rb (OptionParser#make_switch): char class option
      cannot be NoArgument, default to RequiredArgument.

  Added files:
    trunk/test/optparse/test_cclass.rb
  Modified files:
    trunk/ChangeLog
    trunk/lib/optparse.rb
Index: test/optparse/test_cclass.rb
===================================================================
--- test/optparse/test_cclass.rb	(revision 0)
+++ test/optparse/test_cclass.rb	(revision 55228)
@@ -0,0 +1,18 @@ https://github.com/ruby/ruby/blob/trunk/test/optparse/test_cclass.rb#L1
+# frozen_string_literal: false
+require_relative 'test_optparse'
+
+class TestOptionParser::CClass < TestOptionParser
+  def test_no_argument
+    flags = []
+    @opt.def_option("-[a-z]") {|x| flags << x}
+    no_error {@opt.parse!(%w"-a")}
+    assert_equal(%w"a", flags)
+  end
+
+  def test_required_argument
+    flags = []
+    @opt.def_option("-[a-z]X") {|x| flags << x}
+    no_error {@opt.parse!(%w"-a")}
+    assert_equal(%w"a", flags)
+  end
+end

Property changes on: test/optparse/test_cclass.rb
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55227)
+++ ChangeLog	(revision 55228)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue May 31 17:28:46 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/optparse.rb (OptionParser::Completion.candidate): get rid of
+	  nil as key names.  [ruby-core:75773] [Bug #12438]
+
+	* lib/optparse.rb (OptionParser#make_switch): char class option
+	  cannot be NoArgument, default to RequiredArgument.
+
 Tue May 31 00:30:11 2016  NAKAMURA Usaku  <usa@r...>
 
 	* ext/socket/raddrinfo.c (host_str, port_str): Use StringValueCStr
Index: lib/optparse.rb
===================================================================
--- lib/optparse.rb	(revision 55227)
+++ lib/optparse.rb	(revision 55228)
@@ -413,7 +413,7 @@ class OptionParser https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L413
       candidates = []
       block.call do |k, *v|
         (if Regexp === k
-           kn = nil
+           kn = "".freeze
            k === key
          else
            kn = defined?(k.id2name) ? k.id2name : k
@@ -1336,6 +1336,7 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1336
     default_pattern = nil
     klass = nil
     q, a = nil
+    has_arg = false
 
     opts.each do |o|
       # argument class
@@ -1414,6 +1415,8 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1415
         if a
           default_style = default_style.guess(arg = a)
           default_pattern, conv = search(:atype, o) unless default_pattern
+        else
+          has_arg = true
         end
         sdesc << "-#{q}"
         short << Regexp.new(q)
@@ -1436,6 +1439,9 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1439
 
     default_pattern, conv = search(:atype, default_style.pattern) unless default_pattern
     if !(short.empty? and long.empty?)
+      if has_arg and default_style == Switch::NoArgument
+        default_style = Switch::RequiredArgument
+      end
       s = (style || default_style).new(pattern || default_pattern,
                                        conv, sdesc, ldesc, arg, desc, block)
     elsif !block

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

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