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

ruby-changes:73931

From: Maciek <ko1@a...>
Date: Sat, 8 Oct 2022 19:30:50 +0900 (JST)
Subject: [ruby-changes:73931] 0f231f2bab (master): [ruby/optparse] Don't treat empty string as an option description

https://git.ruby-lang.org/ruby.git/commit/?id=0f231f2bab

From 0f231f2babf314593bcd233f4f5b8de8dc936145 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Maciek=20Rz=C4=85sa?= <maciejrzasa@g...>
Date: Sat, 8 Oct 2022 12:30:12 +0200
Subject: [ruby/optparse] Don't treat empty string as an option description

https://github.com/ruby/optparse/commit/078638ee6d
---
 lib/optparse.rb               |  2 +-
 test/optparse/test_summary.rb | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/lib/optparse.rb b/lib/optparse.rb
index 1d42c79045..9dad2e4eda 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -1502,7 +1502,7 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1502
         style = notwice(default_style.guess(arg = o), style, 'style')
         default_pattern, conv = search(:atype, Object) unless default_pattern
       else
-        desc.push(o)
+        desc.push(o) if o && !o.empty?
       end
     end
 
diff --git a/test/optparse/test_summary.rb b/test/optparse/test_summary.rb
index 6b36ce3c76..b5dcb3524e 100644
--- a/test/optparse/test_summary.rb
+++ b/test/optparse/test_summary.rb
@@ -55,4 +55,27 @@ class TestOptionParserSummaryTest < TestOptionParser https://github.com/ruby/ruby/blob/trunk/test/optparse/test_summary.rb#L55
     o.release = "rel"
     assert_equal "foo 0.1 (rel)", o.ver
   end
+
+  # https://github.com/ruby/optparse/issues/37
+  def test_very_long_without_short
+    o = OptionParser.new do |opts|
+      # This causes TypeError
+      opts.on('',   '--long-long-option-param-without-short', "Error desc") { options[:long_long_option_param_without_short] = true }
+      opts.on('',   '--long-option-param', "Long desc") { options[:long_option_param_without_short] = true }
+      opts.on('-a', '--long-long-option-param-with-short', "Normal description") { options[:long_long_option_param_with_short] = true }
+
+      opts.on('',   '--long-long-option-param-without-short-but-with-desc', 'Description of the long long param') { options[:long_long_option_param_without_short_but_with_desc] = true }
+    end
+
+    s = o.summarize
+
+    assert_match(/^\s*--long-long-option-param-without-short$/, s[0])
+    assert_match(/^\s*Error desc$/, s[1])
+    assert_match(/^\s*--long-option-param\s+Long desc$/, s[2])
+    assert_match(/^\s*-a\s+Normal description$/, s[3])
+    assert_match(/^\s*--long-long-option-param-with-short$/, s[4])
+
+    assert_match(/^\s*--long-long-option-param-without-short-but-with-desc$/, s[5])
+    assert_match(/^\s*Description of the long long param$/, s[6])
+  end
 end
-- 
cgit v1.2.1


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

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