Feature #11523 ยป optparse-require-exact.patch
| lib/optparse.rb | ||
|---|---|---|
|
@summary_width = width
|
||
|
@summary_indent = indent
|
||
|
@default_argv = ARGV
|
||
|
@require_exact = false
|
||
|
add_officious
|
||
|
yield self if block_given?
|
||
|
end
|
||
| ... | ... | |
|
# 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.
|
||
|
#
|
||
| ... | ... | |
|
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
|
||
| ... | ... | |
|
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)
|
||
| test/optparse/test_optparse.rb | ||
|---|---|---|
|
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
|
||
|
assert_equal(true, @verbose)
|
||
|
end
|
||
|
def test_require_exact
|
||
|
@opt.def_option('-F', '--irs=IRS', 'irs')
|
||
|
%w(--irs --ir --i -ifoo -i -F -Ffoo).each do |arg|
|
||
|
result = {}
|
||
|
@opt.parse([arg, 'foo'], into: result)
|
||
|
assert_equal({irs: 'foo'}, result)
|
||
|
end
|
||
|
@opt.require_exact = true
|
||
|
%w(--irs -F -Ffoo).each do |arg|
|
||
|
result = {}
|
||
|
@opt.parse([arg, 'foo'], into: result)
|
||
|
assert_equal({irs: 'foo'}, result)
|
||
|
end
|
||
|
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(--ir foo))}
|
||
|
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(--i foo))}
|
||
|
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-irs foo))}
|
||
|
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-ir foo))}
|
||
|
assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-i foo))}
|
||
|
end
|
||
|
end
|
||