Skip to content

Commit

Permalink
fix: Fixed argument parsing to allow a flag value with a newline deli…
Browse files Browse the repository at this point in the history
…mited by = (#258)
  • Loading branch information
dazuma authored May 15, 2024
1 parent 4c62049 commit a922cf3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion toys-core/lib/toys/arg_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def check_flag(arg)
case arg
when "--"
@flags_allowed = false
when /\A(--\w[?\w-]*)=(.*)\z/
when /\A(--\w[?\w-]*)=(.*)\z/m
handle_valued_flag(::Regexp.last_match(1), ::Regexp.last_match(2))
when /\A--.+\z/
handle_plain_flag(arg)
Expand Down
16 changes: 16 additions & 0 deletions toys-core/test/test_arg_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,22 @@ def assert_errors_include(expected, errors)
arg_parser.finish
assert_errors_include('Flag "--aa" is missing a value.', arg_parser.errors)
end

it "allows an = value with a newline" do
tool.add_flag(:a, ["-a", "--aa=VALUE"])
arg_parser.parse(["--aa=hi\nho"])
arg_parser.finish
assert_data_includes({a: "hi\nho"}, arg_parser.data)
assert_empty(arg_parser.errors)
end

it "allows a separate value with a newline" do
tool.add_flag(:a, ["-a", "--aa=VALUE"])
arg_parser.parse(["--aa", "hi\nho"])
arg_parser.finish
assert_data_includes({a: "hi\nho"}, arg_parser.data)
assert_empty(arg_parser.errors)
end
end

describe "optional value flag" do
Expand Down

0 comments on commit a922cf3

Please sign in to comment.