diff options
author | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2021-11-11 17:16:21 +0900 |
---|---|---|
committer | nagachika <nagachika@ruby-lang.org> | 2021-11-22 10:51:35 +0900 |
commit | e735773fd4a0f9cbab82134e22d989bf540b744e (patch) | |
tree | b02cae8321278f8ad9400bc6cb3df05511192d1a /lib | |
parent | b2b66ede714e52b63211bc06996a8a4f237d51e2 (diff) |
Bump optparse version to 0.1.1
Diffstat (limited to 'lib')
-rw-r--r-- | lib/optparse.rb | 61 | ||||
-rw-r--r-- | lib/optparse/kwargs.rb | 3 | ||||
-rw-r--r-- | lib/optparse/optparse.gemspec | 4 |
3 files changed, 53 insertions, 15 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb index bc0e821460..598ebd12bd 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -72,10 +72,10 @@ # 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 @@ # 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 @@ # 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 @@ # 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 @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 # 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 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 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 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,12 +1545,18 @@ XXX 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. # def on_tail(*opts, &block) @@ -1583,6 +1607,9 @@ XXX 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 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 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 ed58cc142b..5a2def4747 100644 --- a/lib/optparse/kwargs.rb +++ b/lib/optparse/kwargs.rb @@ -2,6 +2,9 @@ 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 afb90420f0..831c787ac7 100644 --- a/lib/optparse/optparse.gemspec +++ b/lib/optparse/optparse.gemspec @@ -23,7 +23,9 @@ Gem::Specification.new do |spec| 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) } |