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

Pattern matching #243

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
8 changes: 6 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], [], []} ->
usage_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 @@ -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 <dir>\tChange working dir to <dir> before running
""")
end
Expand Down
17 changes: 17 additions & 0 deletions test/onigumo_cli_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ defmodule OnigumoCLITest do
"-c"
]

@invalid_switches_combinations [
"--help -C invalid",
"-h --invalid"
]

@working_dir_switches [
"--working-dir",
"-C"
Expand Down Expand Up @@ -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 ")
Expand Down
Loading