From 6811b1a68f9b16d0fd778c5d4744afdde034ba6b Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 9 Nov 2024 20:55:49 +0100 Subject: [PATCH] Help message on invalid component Passing an invalid component name prints a friendly help message instead of crashing with a stack trace. This is now consistent with a situation when an invalid switch is passed. --- lib/cli.ex | 9 ++++++--- test/onigumo_cli_test.exs | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 2a66d1f..bd02577 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -10,9 +10,12 @@ defmodule Onigumo.CLI do strict: [working_dir: :string] ) do {switches, [component], []} -> - {:ok, module} = Map.fetch(@components, String.to_atom(component)) - working_dir = Keyword.get(switches, :working_dir, File.cwd!()) - module.main(working_dir) + with {:ok, module} <- Map.fetch(@components, String.to_atom(component)) do + working_dir = Keyword.get(switches, :working_dir, File.cwd!()) + module.main(working_dir) + else + :error -> usage_message() + end _ -> usage_message() diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index c0d51ba..cefab8d 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -21,7 +21,7 @@ defmodule OnigumoCLITest do describe("Onigumo.CLI.main/1") do for argument <- @invalid_arguments do test("run CLI with invalid argument #{inspect(argument)}") do - assert_raise(MatchError, fn -> Onigumo.CLI.main([unquote(argument)]) end) + assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(argument)]) end) end end