Nobuyoshi Nakada 2022-10-30 00:36:19 +09:00 коммит произвёл git
Родитель dcf94e719c
Коммит 0bfb185654
2 изменённых файлов: 19 добавлений и 0 удалений

Просмотреть файл

@ -1148,6 +1148,7 @@ XXX
@summary_indent = indent @summary_indent = indent
@default_argv = ARGV @default_argv = ARGV
@require_exact = false @require_exact = false
@raise_unknown = true
add_officious add_officious
yield self if block_given? yield self if block_given?
end end
@ -1225,6 +1226,9 @@ XXX
# abbreviated long option as short option). # abbreviated long option as short option).
attr_accessor :require_exact attr_accessor :require_exact
# Whether to raise at unknown option.
attr_accessor :raise_unknown
# #
# Heading banner preceding summary. # Heading banner preceding summary.
# #
@ -1639,9 +1643,11 @@ XXX
begin begin
sw, = complete(:long, opt, true) sw, = complete(:long, opt, true)
if require_exact && !sw.long.include?(arg) if require_exact && !sw.long.include?(arg)
throw :terminate, arg unless raise_unknown
raise InvalidOption, arg raise InvalidOption, arg
end end
rescue ParseError rescue ParseError
throw :terminate, arg unless raise_unknown
raise $!.set_option(arg, true) raise $!.set_option(arg, true)
end end
begin begin
@ -1673,6 +1679,7 @@ XXX
end end
end end
rescue ParseError rescue ParseError
throw :terminate, arg unless raise_unknown
raise $!.set_option(arg, true) raise $!.set_option(arg, true)
end end
begin begin

Просмотреть файл

@ -98,6 +98,18 @@ class TestOptionParser < Test::Unit::TestCase
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-z foo))} assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-z foo))}
end end
def test_raise_unknown
@opt.def_option('--foo [ARG]') {|arg| @foo = arg}
assert @opt.raise_unknown
@opt.raise_unknown = false
assert_equal(%w[--bar], @opt.parse(%w[--foo --bar]))
assert_nil(@foo)
assert_equal(%w[--bar], @opt.parse(%w[--foo x --bar]))
assert_equal("x", @foo)
end
def test_nonopt_pattern def test_nonopt_pattern
@opt.def_option(/^[^-]/) do |arg| @opt.def_option(/^[^-]/) do |arg|
assert(false, "Never gets called") assert(false, "Never gets called")