[ruby/optparse] Fix completion of key-value pairs array

Enum array may be the list of pairs of key and value.  Check if only
key is completable, not pair.

Fix https://github.com/ruby/optparse/pull/93
Fix https://github.com/ruby/optparse/pull/94

https://github.com/ruby/optparse/commit/a8d0ba8dac
This commit is contained in:
Nobuyoshi Nakada 2025-03-17 19:13:47 +09:00 committed by git
parent b5cdbadeed
commit 8f19f0aad5
2 changed files with 7 additions and 1 deletions

View File

@ -1502,7 +1502,7 @@ XXX
block = notwice(o, block, 'block')
when Array, Hash
if Array === o
o, v = o.partition {|v| Completion.completable?(v)}
o, v = o.partition {|v,| Completion.completable?(v)}
values = notwice(v, values, 'values') unless v.empty?
next if o.empty?
end

View File

@ -8,6 +8,7 @@ class TestOptionParserPlaceArg < TestOptionParser
@opt.def_option("--option [VAL]") {|x| @flag = x}
@opt.def_option("-T [level]", /^[0-4]$/, Integer) {|x| @topt = x}
@opt.def_option("--enum [VAL]", [:Alpha, :Bravo, :Charlie]) {|x| @enum = x}
@opt.def_option("--enumval [VAL]", [[:Alpha, 1], [:Bravo, 2], [:Charlie, 3]]) {|x| @enum = x}
@opt.def_option("--integer [VAL]", Integer, [1, 2, 3]) {|x| @integer = x}
@opt.def_option("--range [VAL]", Integer, 1..3) {|x| @range = x}
@topt = nil
@ -102,6 +103,11 @@ class TestOptionParserPlaceArg < TestOptionParser
assert_equal(:Alpha, @enum)
end
def test_enum_pair
assert_equal([], no_error {@opt.parse!(%w"--enumval=A")})
assert_equal(1, @enum)
end
def test_enum_conversion
assert_equal([], no_error {@opt.parse!(%w"--integer=1")})
assert_equal(1, @integer)