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

ruby-changes:65694

From: Jeremy <ko1@a...>
Date: Mon, 29 Mar 2021 19:42:00 +0900 (JST)
Subject: [ruby-changes:65694] eca8ffaa0b (master): [ruby/optparse] Add OptionParser#require_exact accessor

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

From eca8ffaa0b446db0a1cacc82a2e73155f6fd3fce Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Mon, 1 Jul 2019 15:19:16 -0700
Subject: [ruby/optparse] Add OptionParser#require_exact accessor

This allows you to disable allowing abbreviations of long options
and using short options for long options.

Implements Ruby Feature #11523

https://github.com/ruby/optparse/commit/dfefb2d2e2
---
 lib/optparse.rb                |  9 +++++++++
 test/optparse/test_optparse.rb | 22 ++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/lib/optparse.rb b/lib/optparse.rb
index bc0e821..af12e42 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -1091,6 +1091,7 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1091
     @summary_width = width
     @summary_indent = indent
     @default_argv = ARGV
+    @require_exact = false
     add_officious
     yield self if block_given?
   end
@@ -1164,6 +1165,10 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1165
   # Strings to be parsed in default.
   attr_accessor :default_argv
 
+  # Whether to require that options match exactly (disallows providing
+  # abbreviated long option as short option).
+  attr_accessor :require_exact
+
   #
   # Heading banner preceding summary.
   #
@@ -1583,6 +1588,9 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1588
           opt.tr!('_', '-')
           begin
             sw, = complete(:long, opt, true)
+            if require_exact && !sw.long.include?(arg)
+              raise InvalidOption, arg
+            end
           rescue ParseError
             raise $!.set_option(arg, true)
           end
@@ -1607,6 +1615,7 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1615
                 val = arg.delete_prefix('-')
                 has_arg = true
               rescue InvalidOption
+                raise if require_exact
                 # if no short options match, try completion with long
                 # options.
                 sw, = complete(:long, opt)
diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb
index e4aeb07..fec14fc 100644
--- a/test/optparse/test_optparse.rb
+++ b/test/optparse/test_optparse.rb
@@ -75,4 +75,26 @@ class TestOptionParser < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/optparse/test_optparse.rb#L75
     assert_equal({host: "localhost", port: 8000, verbose: true}, result)
     assert_equal(true, @verbose)
   end
+
+  def test_require_exact
+    @opt.def_option('-F', '--zrs=IRS', 'zrs')
+    %w(--zrs --zr --z -zfoo -z -F -Ffoo).each do |arg|
+      result = {}
+      @opt.parse([arg, 'foo'], into: result)
+      assert_equal({zrs: 'foo'}, result)
+    end
+
+    @opt.require_exact = true
+    %w(--zrs -F -Ffoo).each do |arg|
+      result = {}
+      @opt.parse([arg, 'foo'], into: result)
+      assert_equal({zrs: 'foo'}, result)
+    end
+
+    assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(--zr foo))}
+    assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(--z foo))}
+    assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-zrs foo))}
+    assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-zr foo))}
+    assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-z foo))}
+  end
 end
-- 
cgit v1.1


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

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