diff --git a/deps/rabbitmq_cli/lib/rabbitmqctl.ex b/deps/rabbitmq_cli/lib/rabbitmqctl.ex index e3696aa4cc..d1640b1ac2 100644 --- a/deps/rabbitmq_cli/lib/rabbitmqctl.ex +++ b/deps/rabbitmq_cli/lib/rabbitmqctl.ex @@ -46,6 +46,22 @@ defmodule RabbitMQCtl do |> handle_shutdown end + def exec_command([] = unparsed_command, _) do + {_args, parsed_options, _} = Parser.parse_global(unparsed_command) + + # this invocation is considered to be invalid. curl and grep do the + # same thing. + {:error, ExitCodes.exit_usage(), HelpCommand.all_usage(parsed_options)}; + end + + def exec_command(["--help"] = unparsed_command, _) do + {_args, parsed_options, _} = Parser.parse_global(unparsed_command) + + # the user asked for --help and we are displaying it to her, + # reporting a success + {:ok, ExitCodes.exit_ok(), HelpCommand.all_usage(parsed_options)}; + end + def exec_command(unparsed_command, output_fun) do {command, command_name, arguments, parsed_options, invalid} = Parser.parse(unparsed_command) @@ -83,7 +99,7 @@ defmodule RabbitMQCtl do case options[:help] do true -> - {:error, ExitCodes.exit_ok(), HelpCommand.all_usage(command, options)}; + {:ok, ExitCodes.exit_ok(), HelpCommand.all_usage(command, options)}; _ -> {arguments, options} = command.merge_defaults(arguments, options) @@ -183,7 +199,7 @@ defmodule RabbitMQCtl do exit_program(exit_code) end - defp handle_shutdown({:error, exit_code, output}) do + defp handle_shutdown({_, exit_code, output}) do device = output_device(exit_code) for line <- List.flatten([output]) do diff --git a/deps/rabbitmq_cli/test/rabbitmqctl_test.exs b/deps/rabbitmq_cli/test/rabbitmqctl_test.exs index 2f7c166e0d..e199ef2043 100644 --- a/deps/rabbitmq_cli/test/rabbitmqctl_test.exs +++ b/deps/rabbitmq_cli/test/rabbitmqctl_test.exs @@ -62,15 +62,29 @@ defmodule RabbitMQCtlTest do delete_user "kirk" end -## ------------------------ Malformed Commands -------------------------------- +## ------------------------ Help and Malformed Commands -------------------------------- - test "Empty command shows usage message" do + test "when invoked without arguments, displays a generic usage message and exits with a non-zero code" do command = [] assert capture_io(:stderr, fn -> error_check(command, exit_usage()) end) =~ ~r/\nUsage:\n/ end + test "when invoked with only a --help, shows a generic usage message and exits with a success" do + command = ["--help"] + assert capture_io(:stdio, fn -> + error_check(command, exit_ok()) + end) =~ ~r/\nUsage:\n/ + end + + test "when invoked with --help [command], shows a generic usage message and exits with a success" do + command = ["--help", "status"] + assert capture_io(:stdio, fn -> + error_check(command, exit_ok()) + end) =~ ~r/\nUsage:\n/ + end + test "Empty command with options shows usage, and exit with usage exit code" do command = ["-n", "sandwich@pastrami"] assert capture_io(:stderr, fn ->