diff --git a/lib/cli.ex b/lib/cli.ex index 6a76a11..bb0aee2 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -6,9 +6,12 @@ defmodule Onigumo.CLI do def main(argv) do case OptionParser.parse( argv, - aliases: [C: :working_dir], - strict: [working_dir: :string] + aliases: [h: :help, C: :working_dir], + strict: [help: :boolean, working_dir: :string] ) do + {[help: true], [], []} -> + help_message() + {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) working_dir = Keyword.get(parsed_switches, :working_dir, File.cwd!()) @@ -20,6 +23,15 @@ defmodule Onigumo.CLI do end defp usage_message() do + IO.puts(""" + onigumo: invalid usage + Usage: onigumo [OPTION]... [COMPONENT] + + Try `onigumo --help' for more options. + """) + end + + defp help_message() do components = Enum.join(Map.keys(@components), ", ") IO.puts(""" @@ -30,6 +42,7 @@ defmodule Onigumo.CLI do COMPONENT\tOnigumo component to run, available: #{components} OPTIONS: + -h, --help\t\tprint this help -C, --working-dir \tChange working dir to before running """) end diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index c0d51ba..8f0f81a 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -13,6 +13,12 @@ defmodule OnigumoCLITest do "-c" ] + @invalid_switch_combinations [ + "--help -C invalid", + "-h --invalid", + "downloader -h" + ] + @working_dir_switches [ "--working-dir", "-C" @@ -39,6 +45,12 @@ defmodule OnigumoCLITest do end end + for switches <- @invalid_switch_combinations do + test("run invalid combination of switches #{inspect(switches)} ") do + assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switches)]) end) + end + end + @tag :tmp_dir test("run CLI with 'downloader' argument passing cwd", %{tmp_dir: tmp_dir}) do expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) @@ -60,9 +72,20 @@ defmodule OnigumoCLITest do end end + for switch <- ["-h", "--help"] do + test("run CLI with a #{inspect(switch)} switch") do + assert help_message_printed?(fn -> Onigumo.CLI.main([unquote(switch)]) end) + end + end + defp usage_message_printed?(function) do output = capture_io(function) - String.starts_with?(output, "Usage: onigumo ") + String.starts_with?(output, "onigumo: invalid usage") + end + + defp help_message_printed?(function) do + output = capture_io(function) + String.starts_with?(output, "Usage: onigumo [OPTION]... [COMPONENT]") end end end