From 52c0557e28795a5e72c5f0dd61d1e8e039104340 Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 16 Aug 2024 19:06:35 +0200 Subject: [PATCH 01/10] 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 02/10] 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 03/10] 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 04/10] 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 05/10] 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 06/10] 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 07/10] 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 From feaaa75747e05fcfad1d84b660fe6e19397ea580 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 25 Aug 2024 19:14:56 +0200 Subject: [PATCH 08/10] move test of invalid switch combination and fix name of constant for these combinations --- test/onigumo_cli_test.exs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 19a2310..473aed9 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -13,7 +13,7 @@ defmodule OnigumoCLITest do "-c" ] - @invalid_switches_combinations [ + @invalid_switch_combinations [ "--help -C invalid", "-h --invalid" ] @@ -44,6 +44,12 @@ defmodule OnigumoCLITest do end end + for switches <- @invalid_switch_combinations do + test("run invalid combination of switches #{inspect(switches)} ") do + 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) @@ -71,12 +77,6 @@ defmodule OnigumoCLITest do 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 ") From 3703cdbd2ef2faf92a710ac3dd7919b8eb10751b Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 25 Aug 2024 21:58:16 +0200 Subject: [PATCH 09/10] Test switch help as last one with downloader component to invoke usage message --- test/onigumo_cli_test.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 473aed9..55140e9 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -15,7 +15,8 @@ defmodule OnigumoCLITest do @invalid_switch_combinations [ "--help -C invalid", - "-h --invalid" + "-h --invalid", + "downloader -h" ] @working_dir_switches [ From d8372eb6513dedb9ac1dab82053aeaaae04d5b9d Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 8 Sep 2024 18:45:07 +0200 Subject: [PATCH 10/10] Capitalize help message sentence for consistency --- lib/cli.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli.ex b/lib/cli.ex index 1d80d50..567c156 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\tprint this help + -h, --help\t\tPrint this help -C, --working-dir \tChange working dir to before running """) end