Accept -t/--timeout before command name [again]

There are existing users (including RabbitMQ core tests) that
use that sequence because it was previously in the docs.

This relaxes command validation so that if a command-specific
flag overrides a global, we accept it as long as their types match.

Per discussion with @hairyhum.
This commit is contained in:
Michael Klishin 2018-03-09 20:28:26 +03:00
parent ce4c53830e
commit 56991de0f3
2 changed files with 27 additions and 4 deletions

View File

@ -168,6 +168,7 @@ defmodule RabbitMQ.CLI.Core.Parser do
quiet: :boolean,
dry_run: :boolean,
vhost: :string,
timeout: :integer,
longnames: :boolean,
formatter: :string,
printer: :string,
@ -186,7 +187,8 @@ defmodule RabbitMQ.CLI.Core.Parser do
[p: :vhost,
n: :node,
q: :quiet,
l: :longnames]
l: :longnames,
t: :timeout]
end
defp build_switches(default, command) do
@ -204,7 +206,7 @@ defmodule RabbitMQ.CLI.Core.Parser do
{command, {:redefining_global_aliases,
command_aliases}}})
end
defp apply_if_exported(mod, fun, args, default) do
case function_exported?(mod, fun, length(args)) do
true -> apply(mod, fun, args);
@ -214,8 +216,17 @@ defmodule RabbitMQ.CLI.Core.Parser do
defp merge_if_different(default, specific, error) do
case keyword_intersect(default, specific) do
[] -> Keyword.merge(default, specific);
_ -> exit(error)
[] -> Keyword.merge(default, specific);
conflicts ->
# if all conflicting keys are of the same type,
# that's acceptable
case Enum.all?(conflicts,
fn(c) ->
Keyword.get(default, c) == Keyword.get(specific, c)
end) do
true -> Keyword.merge(default, specific);
false -> exit(error)
end
end
end

View File

@ -152,6 +152,18 @@ defmodule ParserTest do
{["seagull"], %{vhost: "my_vhost"}, []}
end
test "--timeout can be specified before command" do
# for backwards compatibility
assert @subject.parse_global(["-n", "rabbitmq@localhost", "--timeout", "5", "sandwich", "pastrami"]) ==
{["sandwich", "pastrami"], %{node: :"rabbitmq@localhost", timeout: 5}, []}
end
test "-t can be specified before command" do
# for backwards compatibility
assert @subject.parse_global(["-n", "rabbitmq@localhost", "-t", "5", "sandwich", "pastrami"]) ==
{["sandwich", "pastrami"], %{node: :"rabbitmq@localhost", timeout: 5}, []}
end
test "parse/1 returns command name" do
command_line = ["pacific_gull", "fly", "-p", "my_vhost"]
command = RabbitMQ.CLI.Seagull.Commands.PacificGullCommand