[ruby/optparse] Also accept '-' as an optional argument (https://github.com/ruby/optparse/pull/35)

https://github.com/ruby/optparse/commit/f2b8318631
This commit is contained in:
konsolebox 2022-06-09 19:43:24 +08:00 коммит произвёл Hiroshi SHIBATA
Родитель 3725454161
Коммит 419ad1e13e
2 изменённых файлов: 9 добавлений и 3 удалений

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

@ -765,15 +765,15 @@ class OptionParser
end end
# #
# Switch that takes an argument, which does not begin with '-'. # Switch that takes an argument, which does not begin with '-' or is '-'.
# #
class PlacedArgument < self class PlacedArgument < self
# #
# Returns nil if argument is not present or begins with '-'. # Returns nil if argument is not present or begins with '-' and is not '-'.
# #
def parse(arg, argv, &error) def parse(arg, argv, &error)
if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0])) if !(val = arg) and (argv.empty? or /\A-./ =~ (val = argv[0]))
return nil, block, nil return nil, block, nil
end end
opt = (val = parse_arg(val, &error))[1] opt = (val = parse_arg(val, &error))[1]

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

@ -18,6 +18,8 @@ class TestOptionParserPlaceArg < TestOptionParser
def test_short def test_short
assert_equal(%w"", no_error {@opt.parse!(%w"-x -n")}) assert_equal(%w"", no_error {@opt.parse!(%w"-x -n")})
assert_equal(nil, @flag) assert_equal(nil, @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"-x -")})
assert_equal("-", @flag)
@flag = false @flag = false
assert_equal(%w"", no_error {@opt.parse!(%w"-x foo")}) assert_equal(%w"", no_error {@opt.parse!(%w"-x foo")})
assert_equal("foo", @flag) assert_equal("foo", @flag)
@ -30,6 +32,8 @@ class TestOptionParserPlaceArg < TestOptionParser
def test_abbrev def test_abbrev
assert_equal(%w"", no_error {@opt.parse!(%w"-o -n")}) assert_equal(%w"", no_error {@opt.parse!(%w"-o -n")})
assert_equal(nil, @flag) assert_equal(nil, @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"-o -")})
assert_equal("-", @flag)
@flag = false @flag = false
assert_equal(%w"", no_error {@opt.parse!(%w"-o foo")}) assert_equal(%w"", no_error {@opt.parse!(%w"-o foo")})
assert_equal("foo", @flag) assert_equal("foo", @flag)
@ -42,6 +46,8 @@ class TestOptionParserPlaceArg < TestOptionParser
def test_long def test_long
assert_equal(%w"", no_error {@opt.parse!(%w"--opt -n")}) assert_equal(%w"", no_error {@opt.parse!(%w"--opt -n")})
assert_equal(nil, @flag) assert_equal(nil, @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--opt -")})
assert_equal("-", @flag)
assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt= foo")}) assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt= foo")})
assert_equal("", @flag) assert_equal("", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")}) assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")})