From 52c0557e28795a5e72c5f0dd61d1e8e039104340 Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 16 Aug 2024 19:06:35 +0200 Subject: [PATCH 1/7] Implement switch --help to cli --- lib/cli.ex | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 6a76a11..4178d0a 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -6,9 +6,13 @@ 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(); + IO.puts("AAAA") + {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) working_dir = Keyword.get(parsed_switches, :working_dir, File.cwd!()) @@ -30,6 +34,7 @@ defmodule Onigumo.CLI do COMPONENT\tOnigumo component to run, available: #{components} OPTIONS: + -h, --help\t\tusage help -C, --working-dir \tChange working dir to before running """) end From 7d529a5d64e429055d12d3d088000518256c0062 Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 16 Aug 2024 19:12:24 +0200 Subject: [PATCH 2/7] Fix typo with semicolon and remove debug print --- lib/cli.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 4178d0a..6ca97e9 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -10,8 +10,7 @@ defmodule Onigumo.CLI do strict: [help: :boolean, working_dir: :string] ) do {[help: true], [], []} -> - usage_message(); - IO.puts("AAAA") + usage_message() {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) From 16bc7227d7ed02041f2e8d3ac63a940abf1ee4eb Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 16 Aug 2024 20:01:11 +0200 Subject: [PATCH 3/7] add tests for ---help switch --- test/onigumo_cli_test.exs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index c0d51ba..6299ade 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -60,6 +60,12 @@ defmodule OnigumoCLITest do end end + for switch <- ["-h", "--help", "--help -C prdel", "-h --invalid-switch"] do + test("run CLI with #{inspect(switch)} switch(es)") do + assert usage_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 ") From 9b2cebc6f23b7505d3ab4ff3588ddc970d4934bd Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 25 Aug 2024 17:33:41 +0200 Subject: [PATCH 4/7] Split test to use valid help switch and invalid combination with another switch once --- test/onigumo_cli_test.exs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 6299ade..d8c8259 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -60,12 +60,18 @@ defmodule OnigumoCLITest do end end - for switch <- ["-h", "--help", "--help -C prdel", "-h --invalid-switch"] do - test("run CLI with #{inspect(switch)} switch(es)") do + 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 + 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 ") From 79dad906d51f2c909c0b85a6f891173d5e6d3833 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 25 Aug 2024 17:44:57 +0200 Subject: [PATCH 5/7] Update test for help switch in usage message --- lib/cli.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli.ex b/lib/cli.ex index 6ca97e9..1d80d50 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -33,7 +33,7 @@ defmodule Onigumo.CLI do COMPONENT\tOnigumo component to run, available: #{components} OPTIONS: - -h, --help\t\tusage help + -h, --help\t\tprint this help -C, --working-dir \tChange working dir to before running """) end From d8da52e0498c95e03f2cccb5d176294bdb9c80c6 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 25 Aug 2024 18:01:03 +0200 Subject: [PATCH 6/7] Move invalid combinations of switches to global constant to be able to add new ones in future --- test/onigumo_cli_test.exs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index d8c8259..a019b95 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" @@ -66,7 +71,7 @@ defmodule OnigumoCLITest do end end - for switches <- ["--help -C invalid", "-h --invalid"] do + for switches <- @invalid_switches_combinations do test("run invalid combination of swiches #{inspect(switches)} ") do assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switches)]) end) end From b9c920d39ed0248c4673f11375da45aeeadb1bcd Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 25 Aug 2024 18:01:29 +0200 Subject: [PATCH 7/7] Fix a typo --- test/onigumo_cli_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index a019b95..19a2310 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -72,7 +72,7 @@ defmodule OnigumoCLITest do end for switches <- @invalid_switches_combinations do - test("run invalid combination of swiches #{inspect(switches)} ") do + test("run invalid combination of switches #{inspect(switches)} ") do assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switches)]) end) end end