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

Implement switch -h, --help to CLI #238

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalized, this sentence would be more consistent.

Suggested change
-h, --help\t\tprint this help
-h, --help\t\tPrint this help

-C, --working-dir <dir>\tChange working dir to <dir> before running
""")
end
Expand Down
12 changes: 12 additions & 0 deletions test/onigumo_cli_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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 <- ["--help -C invalid", "-h --invalid"] do
test("run invalid combination of swiches #{inspect(switches)} ") do
nappex marked this conversation as resolved.
Show resolved Hide resolved
nappex marked this conversation as resolved.
Show resolved Hide resolved
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