diff --git a/lib/cli.ex b/lib/cli.ex index 6a76a11..1d80d50 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], [], []} -> + usage_message() + {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) working_dir = Keyword.get(parsed_switches, :working_dir, File.cwd!()) @@ -30,6 +33,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..19a2310 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -13,6 +13,11 @@ defmodule OnigumoCLITest do "-c" ] + @invalid_switches_combinations [ + "--help -C invalid", + "-h --invalid" + ] + @working_dir_switches [ "--working-dir", "-C" @@ -60,6 +65,18 @@ defmodule OnigumoCLITest do end end + for switch <- ["-h", "--help"] do + test("run CLI with a #{inspect(switch)} switch") do + assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switch)]) end) + end + end + + for switches <- @invalid_switches_combinations do + test("run invalid combination of switches #{inspect(switches)} ") do + assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switches)]) end) + end + end + defp usage_message_printed?(function) do output = capture_io(function) String.starts_with?(output, "Usage: onigumo ")