mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/optparse] Add OptionParser#require_exact accessor
This allows you to disable allowing abbreviations of long options and using short options for long options. Implements Ruby Feature #11523 https://github.com/ruby/optparse/commit/dfefb2d2e2
This commit is contained in:
parent
d474b19b5b
commit
eca8ffaa0b
2 changed files with 31 additions and 0 deletions
|
@ -1091,6 +1091,7 @@ XXX
|
||||||
@summary_width = width
|
@summary_width = width
|
||||||
@summary_indent = indent
|
@summary_indent = indent
|
||||||
@default_argv = ARGV
|
@default_argv = ARGV
|
||||||
|
@require_exact = false
|
||||||
add_officious
|
add_officious
|
||||||
yield self if block_given?
|
yield self if block_given?
|
||||||
end
|
end
|
||||||
|
@ -1164,6 +1165,10 @@ XXX
|
||||||
# Strings to be parsed in default.
|
# Strings to be parsed in default.
|
||||||
attr_accessor :default_argv
|
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.
|
# Heading banner preceding summary.
|
||||||
#
|
#
|
||||||
|
@ -1583,6 +1588,9 @@ XXX
|
||||||
opt.tr!('_', '-')
|
opt.tr!('_', '-')
|
||||||
begin
|
begin
|
||||||
sw, = complete(:long, opt, true)
|
sw, = complete(:long, opt, true)
|
||||||
|
if require_exact && !sw.long.include?(arg)
|
||||||
|
raise InvalidOption, arg
|
||||||
|
end
|
||||||
rescue ParseError
|
rescue ParseError
|
||||||
raise $!.set_option(arg, true)
|
raise $!.set_option(arg, true)
|
||||||
end
|
end
|
||||||
|
@ -1607,6 +1615,7 @@ XXX
|
||||||
val = arg.delete_prefix('-')
|
val = arg.delete_prefix('-')
|
||||||
has_arg = true
|
has_arg = true
|
||||||
rescue InvalidOption
|
rescue InvalidOption
|
||||||
|
raise if require_exact
|
||||||
# if no short options match, try completion with long
|
# if no short options match, try completion with long
|
||||||
# options.
|
# options.
|
||||||
sw, = complete(:long, opt)
|
sw, = complete(:long, opt)
|
||||||
|
|
|
@ -75,4 +75,26 @@ class TestOptionParser < Test::Unit::TestCase
|
||||||
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
|
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
|
||||||
assert_equal(true, @verbose)
|
assert_equal(true, @verbose)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue