Project

General

Profile

Feature #8460 » 0001-Add-keep_unknown-option.patch

Add `keep_unknown` option - felipec (Felipe Contreras), 04/02/2023 06:50 AM

View differences:

lib/optparse.rb
@default_argv = ARGV
@require_exact = false
@raise_unknown = true
@keep_unknown = false
add_officious
yield self if block_given?
end
......
# Whether to raise at unknown option.
attr_accessor :raise_unknown
# Unknown arguments are left alone.
attr_accessor :keep_unknown
#
# Heading banner preceding summary.
#
......
raise InvalidOption, arg
end
rescue ParseError
throw :terminate, arg unless raise_unknown
raise $!.set_option(arg, true)
if !keep_unknown
throw :terminate, arg unless raise_unknown
raise $!.set_option(arg, true)
else
nonopt.call(arg)
next
end
end
begin
opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
......
end
end
rescue ParseError
throw :terminate, arg unless raise_unknown
raise $!.set_option(arg, true)
if !keep_unknown
throw :terminate, arg unless raise_unknown
raise $!.set_option(arg, true)
else
nonopt.call(arg)
next
end
end
begin
opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
test/optparse/test_optparse.rb
e = assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-t))}
assert_equal(["-t"], e.args)
end
def test_keep_unknown
@opt.def_option('-x')
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(--mis))}
@opt.keep_unknown = true
assert_equal(%w(--mis arg), @opt.parse(%w(--mis -x arg)))
end
end
(1-1/3)