Skip to content

Commit

Permalink
Also accept '-' as an optional argument (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
konsolebox committed Jun 9, 2022
1 parent e70e689 commit f2b8318
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/optparse.rb
Expand Up @@ -765,15 +765,15 @@ def pretty_head # :nodoc:
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

#
# 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)
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
end
opt = (val = parse_arg(val, &error))[1]
Expand Down
6 changes: 6 additions & 0 deletions test/optparse/test_placearg.rb
Expand Up @@ -18,6 +18,8 @@ def setup
def test_short
assert_equal(%w"", no_error {@opt.parse!(%w"-x -n")})
assert_equal(nil, @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"-x -")})
assert_equal("-", @flag)
@flag = false
assert_equal(%w"", no_error {@opt.parse!(%w"-x foo")})
assert_equal("foo", @flag)
Expand All @@ -30,6 +32,8 @@ def test_short
def test_abbrev
assert_equal(%w"", no_error {@opt.parse!(%w"-o -n")})
assert_equal(nil, @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"-o -")})
assert_equal("-", @flag)
@flag = false
assert_equal(%w"", no_error {@opt.parse!(%w"-o foo")})
assert_equal("foo", @flag)
Expand All @@ -42,6 +46,8 @@ def test_abbrev
def test_long
assert_equal(%w"", no_error {@opt.parse!(%w"--opt -n")})
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("", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")})
Expand Down

0 comments on commit f2b8318

Please sign in to comment.