From 52c0557e28795a5e72c5f0dd61d1e8e039104340 Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 16 Aug 2024 19:06:35 +0200 Subject: [PATCH 01/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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 f9d0f11088cb6c276bec104bf048453043377094 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 25 Aug 2024 22:13:54 +0200 Subject: [PATCH 10/15] Add help message to CLI --- lib/cli.ex | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/cli.ex b/lib/cli.ex index 1d80d50..fd1213b 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -37,4 +37,20 @@ defmodule Onigumo.CLI do -C, --working-dir \tChange working dir to before running """) end + + defp help_message() do + components = Enum.join(Map.keys(@components), ", ") + + IO.puts(""" + Usage: onigumo [OPTION]... [COMPONENT] + + Simple program that retrieves HTTP web content as structured data. + + COMPONENT\tOnigumo component to run, available: #{components} + + OPTIONS: + -h, --help\t\tprint this help + -C, --working-dir \tChange working dir to before running + """) + end end From 98293ed3a3ad2607256ff9b3c95f5bbd3fea2d6e Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 25 Aug 2024 22:14:52 +0200 Subject: [PATCH 11/15] Simplify usage message --- lib/cli.ex | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index fd1213b..bf39318 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -26,15 +26,10 @@ defmodule Onigumo.CLI do components = Enum.join(Map.keys(@components), ", ") IO.puts(""" + onigumo: invalid usage Usage: onigumo [OPTION]... [COMPONENT] - Simple program that retrieves HTTP web content as structured data. - - COMPONENT\tOnigumo component to run, available: #{components} - - OPTIONS: - -h, --help\t\tprint this help - -C, --working-dir \tChange working dir to before running + Try `onigumo --help' for more options. """) end From f7d0d95de6765512f9f416da5d005b9e3680ceaf Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 8 Sep 2024 19:11:02 +0200 Subject: [PATCH 12/15] Remove variable components has no extra value for usage message --- lib/cli.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index bf39318..520d40a 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -23,8 +23,6 @@ defmodule Onigumo.CLI do end defp usage_message() do - components = Enum.join(Map.keys(@components), ", ") - IO.puts(""" onigumo: invalid usage Usage: onigumo [OPTION]... [COMPONENT] From a6fd9f2b5d4ee01ae891a7dee21bc3a7f4fc9408 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 8 Sep 2024 19:22:36 +0200 Subject: [PATCH 13/15] Split help and usage message in test --- 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 55140e9..8d37000 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -80,7 +80,12 @@ defmodule OnigumoCLITest do 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 From a2be641529a9190c26a05faff0b97e610a98f89c Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 8 Sep 2024 19:23:09 +0200 Subject: [PATCH 14/15] Replace usage msg with help msg in test --- 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 8d37000..8f0f81a 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -74,7 +74,7 @@ defmodule OnigumoCLITest 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) + assert help_message_printed?(fn -> Onigumo.CLI.main([unquote(switch)]) end) end end From 58d5b7a190d6d1cbe0aae9e1cc73145ce8e5edaa Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 8 Sep 2024 19:27:45 +0200 Subject: [PATCH 15/15] Call help msg with -h switch --- lib/cli.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli.ex b/lib/cli.ex index 520d40a..bb0aee2 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -10,7 +10,7 @@ defmodule Onigumo.CLI do strict: [help: :boolean, working_dir: :string] ) do {[help: true], [], []} -> - usage_message() + help_message() {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component))