Skip to content

Commit

Permalink
Help message on invalid component
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Glutexo committed Nov 9, 2024
1 parent fcfea46 commit ba08334
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +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()
end
Expand Down
2 changes: 1 addition & 1 deletion test/onigumo_cli_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ba08334

Please sign in to comment.