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

ruby-changes:69850

From: Hiroshi <ko1@a...>
Date: Mon, 22 Nov 2021 10:52:02 +0900 (JST)
Subject: [ruby-changes:69850] e735773fd4 (ruby_3_0): Bump optparse version to 0.1.1

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

From e735773fd4a0f9cbab82134e22d989bf540b744e Mon Sep 17 00:00:00 2001
From: Hiroshi SHIBATA <hsbt@r...>
Date: Thu, 11 Nov 2021 17:16:21 +0900
Subject: Bump optparse version to 0.1.1

---
 lib/optparse.rb                  | 61 +++++++++++++++++++++++++++++++---------
 lib/optparse/kwargs.rb           |  3 ++
 lib/optparse/optparse.gemspec    |  4 ++-
 test/optparse/test_acceptable.rb |  1 -
 test/optparse/test_optparse.rb   | 30 ++++++++++++++++++++
 5 files changed, 83 insertions(+), 16 deletions(-)

diff --git a/lib/optparse.rb b/lib/optparse.rb
index bc0e8214608..598ebd12bd0 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -72,10 +72,10 @@ https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L72
 #   require 'optparse'
 #
 #   options = {}
-#   OptionParser.new do |opts|
-#     opts.banner = "Usage: example.rb [options]"
+#   OptionParser.new do |parser|
+#     parser.banner = "Usage: example.rb [options]"
 #
-#     opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
+#     parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
 #       options[:verbose] = v
 #     end
 #   end.parse!
@@ -96,15 +96,15 @@ https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L96
 #     def self.parse(options)
 #       args = Options.new("world")
 #
-#       opt_parser = OptionParser.new do |opts|
-#         opts.banner = "Usage: example.rb [options]"
+#       opt_parser = OptionParser.new do |parser|
+#         parser.banner = "Usage: example.rb [options]"
 #
-#         opts.on("-nNAME", "--name=NAME", "Name to say hello to") do |n|
+#         parser.on("-nNAME", "--name=NAME", "Name to say hello to") do |n|
 #           args.name = n
 #         end
 #
-#         opts.on("-h", "--help", "Prints this help") do
-#           puts opts
+#         parser.on("-h", "--help", "Prints this help") do
+#           puts parser
 #           exit
 #         end
 #       end
@@ -241,10 +241,10 @@ https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L241
 #   require 'optparse'
 #
 #   params = {}
-#   OptionParser.new do |opts|
-#     opts.on('-a')
-#     opts.on('-b NUM', Integer)
-#     opts.on('-v', '--verbose')
+#   OptionParser.new do |parser|
+#     parser.on('-a')
+#     parser.on('-b NUM', Integer)
+#     parser.on('-v', '--verbose')
 #   end.parse!(into: params)
 #
 #   p params
@@ -419,7 +419,7 @@ https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L419
 # have any questions, file a ticket at http://bugs.ruby-lang.org.
 #
 class OptionParser
-  OptionParser::Version = "0.1.0"
+  OptionParser::Version = "0.1.1"
 
   # :stopdoc:
   NoArgument = [NO_ARGUMENT = :NONE, nil].freeze
@@ -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.
   #
@@ -1305,13 +1310,16 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1310
   private :notwice
 
   SPLAT_PROC = proc {|*a| a.length <= 1 ? a.first : a} # :nodoc:
+
+  # :call-seq:
+  #   make_switch(params, block = nil)
   #
   # Creates an OptionParser::Switch from the parameters. The parsed argument
   # value is passed to the given block, where it can be processed.
   #
   # See at the beginning of OptionParser for some full examples.
   #
-  # +opts+ can include the following elements:
+  # +params+ can include the following elements:
   #
   # [Argument style:]
   #   One of the following:
@@ -1498,11 +1506,16 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1506
       nolong
   end
 
+  # :call-seq:
+  #   define(*params, &block)
+  #
   def define(*opts, &block)
     top.append(*(sw = make_switch(opts, block)))
     sw[0]
   end
 
+  # :call-seq:
+  #   on(*params, &block)
   #
   # Add option switch and handler. See #make_switch for an explanation of
   # parameters.
@@ -1513,11 +1526,16 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1526
   end
   alias def_option define
 
+  # :call-seq:
+  #   define_head(*params, &block)
+  #
   def define_head(*opts, &block)
     top.prepend(*(sw = make_switch(opts, block)))
     sw[0]
   end
 
+  # :call-seq:
+  #   on_head(*params, &block)
   #
   # Add option switch like with #on, but at head of summary.
   #
@@ -1527,11 +1545,17 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1545
   end
   alias def_head_option define_head
 
+  # :call-seq:
+  #   define_tail(*params, &block)
+  #
   def define_tail(*opts, &block)
     base.append(*(sw = make_switch(opts, block)))
     sw[0]
   end
 
+  #
+  # :call-seq:
+  #   on_tail(*params, &block)
   #
   # Add option switch like with #on, but at tail of summary.
   #
@@ -1583,6 +1607,9 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1607
           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 +1634,7 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1634
                 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)
@@ -1618,7 +1646,12 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1646
           end
           begin
             opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
+          rescue ParseError
+            raise $!.set_option(arg, arg.length > 2)
+          else
             raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
+          end
+          begin
             argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
             val = cb.call(val) if cb
             setter.call(sw.switch_name, val) if setter
diff --git a/lib/optparse/kwargs.rb b/lib/optparse/kwargs.rb
index ed58cc142b6..5a2def47472 100644
--- a/lib/optparse/kwargs.rb
+++ b/lib/optparse/kwargs.rb
@@ -2,6 +2,9 @@ https://github.com/ruby/ruby/blob/trunk/lib/optparse/kwargs.rb#L2
 require 'optparse'
 
 class OptionParser
+  # :call-seq:
+  #   define_by_keywords(options, method, **params)
+  #
   def define_by_keywords(options, meth, **opts)
     meth.parameters.each do |type, name|
       case type
diff --git a/lib/optparse/optparse.gemspec b/lib/optparse/optparse.gemspec
index afb90420f08..831c787ac72 100644
--- a/lib/optparse/optparse.gemspec
+++ b/lib/optparse/optparse.gemspec
@@ -23,7 +23,9 @@ Gem::Specification.new do |spec| https://github.com/ruby/ruby/blob/trunk/lib/optparse/optparse.gemspec#L23
   spec.metadata["source_code_uri"] = spec.homepage
 
   spec.files         = Dir.chdir(File.expand_path('..', __FILE__)) do
-    `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
+    `git ls-files -z`.split("\x0").reject { |f|
+      f.match(%r{\A(?:(?:test|spec|features)/|Gemfile|\.(?:editor|git))})
+    }
   end
   spec.bindir        = "exe"
   spec.executables   = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
diff --git a/test/optparse/test_acceptable.rb b/test/optparse/test_acceptable.rb
index 5c3fbdb10cc..12f78865386 100644
--- a/test/optparse/test_acceptable.rb
+++ b/test/optparse/test_acceptable.rb
@@ -196,4 +196,3 @@ class TestOptionParser::Acceptable < TestOptionParser https://github.com/ruby/ruby/blob/trunk/test/optparse/test_acceptable.rb#L196
   end
 
 end
-
diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb
index e4aeb07aac3..5f5ea183b07 100644
--- a/test/optparse/test_optparse.rb
+++ b/test/optparse/test_optparse.rb
@@ -75,4 +75,34 @@ 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
+
+  def test_nonopt_pattern
+    @opt.def_option(/^[^-]/) do |arg|
+      assert(false, "Never gets called")
+    end
+    e = assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-t))}
+    assert_equal(["-t"], e.args)
+  end
 end
-- 
cgit v1.2.1


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

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