Skip to content

Commit

Permalink
Fix a bug that flag value is processed as flag
Browse files Browse the repository at this point in the history
If flag value starts with "-", unknown option error is raised.

The current flag value check is "orig_arg == opt.value.to_s". There
are some objects such as Regexp and Time that input value and its #to_s
aren't same.
  • Loading branch information
kou committed Jun 25, 2019
1 parent aa233ab commit 7e76b6d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/slop/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def parse(strings)
if opt.expects_argument?

# if we consumed the argument, remove the next pair
if orig_arg == opt.value.to_s
if consume_next_argument?(orig_flag)
pairs.delete_at(idx + 1)
end

Expand Down Expand Up @@ -106,6 +106,13 @@ def unused_options

private

def consume_next_argument?(flag)
return false if /=/ === flag
return true if flag.start_with?("--")
return true if /\A-[a-zA-Z]\z/ === flag
false
end

# We've found an option, process and return it
def process(option, arg)
option.ensure_call(arg)
Expand Down
6 changes: 6 additions & 0 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
assert_equal(-123.987, @result[:multiple])
end

it "parses negative float" do
@options.regexp "--pattern"
@result.parser.parse %w(--pattern -x)
assert_equal(/-x/, @result[:pattern])
end

describe "parsing grouped short flags" do
before do
@options.bool "-q", "--quiet"
Expand Down

0 comments on commit 7e76b6d

Please sign in to comment.