Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split cli messages to case with invalid usage and help message #242

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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!())
Expand All @@ -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("""
Expand All @@ -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 <dir>\tChange working dir to <dir> before running
""")
end
Expand Down
25 changes: 24 additions & 1 deletion test/onigumo_cli_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"-c"
]

@invalid_switch_combinations [
"--help -C invalid",
"-h --invalid",
"downloader -h"
]

@working_dir_switches [
"--working-dir",
"-C"
Expand All @@ -39,6 +45,12 @@
end
end

for switches <- @invalid_switch_combinations do
test("run invalid combination of switches #{inspect(switches)} ") do

Check failure on line 49 in test/onigumo_cli_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

test Onigumo.CLI.main/1 run invalid combination of switches "downloader -h" (OnigumoCLITest)
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)
Expand All @@ -60,9 +72,20 @@
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
Loading