diff --git a/lib/cli.ex b/lib/cli.ex index e9d0c8e..21914f7 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -4,9 +4,28 @@ defmodule Onigumo.CLI do } def main(argv) do - {[], [component]} = OptionParser.parse!(argv, strict: []) - {:ok, module} = Map.fetch(@components, String.to_atom(component)) - root_path = File.cwd!() - module.main(root_path) + try do + {[], [component]} = OptionParser.parse!(argv, strict: []) + component + rescue + _ in [OptionParser.ParseError, MatchError] -> usage_message() + else + component -> + {:ok, module} = Map.fetch(@components, String.to_atom(component)) + root_path = File.cwd!() + module.main(root_path) + end + end + + defp usage_message() do + components = Enum.join(Map.keys(@components), ", ") + + IO.puts(""" + Usage: onigumo [COMPONENT] + + Simple program that retrieves HTTP web content as structured data. + + COMPONENT\tOnigumo component to run, available: #{components} + """) end end diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index a8a193a..d388889 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -1,5 +1,6 @@ defmodule OnigumoCLITest do use ExUnit.Case + import ExUnit.CaptureIO import Mox @urls [ @@ -33,15 +34,20 @@ defmodule OnigumoCLITest do end test("run CLI with no arguments") do - assert_raise(MatchError, fn -> Onigumo.CLI.main([]) end) + assert usage_message_printed?(fn -> Onigumo.CLI.main([]) end) end test("run CLI with more than one argument") do - assert_raise(MatchError, fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) + assert usage_message_printed?(fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) end test("run CLI with invalid switch") do - assert_raise(OptionParser.ParseError, fn -> Onigumo.CLI.main(["--help"]) end) + assert usage_message_printed?(fn -> Onigumo.CLI.main(["--invalid"]) end) + end + + defp usage_message_printed?(function) do + output = capture_io(function) + String.starts_with?(output, "Usage: onigumo ") end end end