1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Bump optparse version to 0.1.1

This commit is contained in:
Hiroshi SHIBATA 2021-11-11 17:16:21 +09:00 committed by nagachika
parent b2b66ede71
commit e735773fd4
5 changed files with 83 additions and 16 deletions

View file

@ -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,11 +1545,17 @@ 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.
#
@ -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

View file

@ -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

View file

@ -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) }

View file

@ -196,4 +196,3 @@ class TestOptionParser::Acceptable < TestOptionParser
end
end

View file

@ -75,4 +75,34 @@ class TestOptionParser < Test::Unit::TestCase
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