Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework argument parsing in do/as providers #2813

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions apps/rebar/src/rebar_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,20 @@ arg_or_flag(["," ++ Task|Rest], Acc) -> new_task([Task|Rest], Acc);
%% a flag
arg_or_flag(["-" ++ _ = Flag|Rest], [{Task, Args}|Acc]) ->
case maybe_ends_in_comma(Flag) of
false -> arg_or_flag(Rest, [{Task, [Flag|Args]}|Acc]);
NewFlag -> new_task(Rest, [{Task,
lists:reverse([NewFlag|Args])}|Acc])
false ->
case rest_arg_self_contains_comma(Rest) of
false ->
arg_or_flag(Rest, [{Task, [Flag|Args]}|Acc]);
{ExtraArg,NewRest} ->
case maybe_ends_in_comma(ExtraArg) of
false ->
arg_or_flag(NewRest, [{Task, [ExtraArg,Flag|Args]}|Acc]);
TrimmedArg ->
new_task(NewRest, [{Task, [Flag,TrimmedArg|Args]}|Acc])
end
end;
NewFlag ->
new_task(Rest, [{Task, lists:reverse([NewFlag|Args])}|Acc])
end;
%% an argument or a sequence of arguments
arg_or_flag([ArgList|Rest], [{Task, Args}|Acc]) ->
Expand All @@ -914,6 +925,23 @@ maybe_ends_in_comma(H) ->
_ -> false
end.

%% looks whether a sequence of arguments comes with a comma inside of it.
%% For example `-a a,b' would match here, but `-a x' or `-a x,' wouldn't.
%% This is used by the caller to know whether to break up a task or assume
%% the comma is part of the argument itself.
rest_arg_self_contains_comma([]) -> false;
rest_arg_self_contains_comma([ArgList|Rest]) ->
case re:split(ArgList, ",", [{return, list}, {parts, 2}, unicode]) of
[_Arg, ""] ->
false;
[_Arg, _More] ->
{ArgList, Rest};
[_Arg] ->
false
end.



get_http_vars(Scheme) ->
OS = case os:getenv(atom_to_list(Scheme)) of
Str when is_list(Str) -> Str;
Expand Down
16 changes: 16 additions & 0 deletions apps/rebar/test/rebar_utils_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
task_with_flag_with_commas/1,
task_with_multiple_flags/1,
special_task_do/1,
special_task_as_with_commas/1,
valid_otp_version/1,
valid_old_format_otp_version/1,
valid_otp_version_equal/1,
Expand Down Expand Up @@ -70,6 +71,7 @@ groups() ->
task_with_flag_with_commas,
task_with_multiple_flags,
special_task_do,
special_task_as_with_commas,
valid_otp_version,
valid_old_format_otp_version,
valid_otp_version_equal,
Expand Down Expand Up @@ -145,6 +147,20 @@ special_task_do(_Config) ->
"do",
"bar,",
"baz"]).
special_task_as_with_commas(_Config) ->
[{"as", ["profile"]}, {"bar", ["--x=y,z"]}, {"baz", ["--arg=a,b"]}] =
rebar_utils:args_to_tasks(["as", "profile,",
"bar", "--x=y,z,",
"baz", "--arg=a,b"]),
[{"as", ["profile"]}, {"bar", ["-x", "y,z"]}, {"baz", ["--a", "a,b"]}] =
rebar_utils:args_to_tasks(["as", "profile,",
"bar", "-x", "y,z,",
"baz", "--a", "a,b"]),
[{"as", ["profile"]}, {"bar", ["-x", "y"]}, {"z", []}, {"baz", ["--a", "a"]}, {"b",[]}] =
rebar_utils:args_to_tasks(["as", "profile,",
"bar", "-x", "y,", "z,",
"baz", "--a", "a,", "b"]),
ok.

valid_otp_version(_Config) ->
meck:new(rebar_utils, [passthrough]),
Expand Down
Loading