From 3d4cca5b8493a458f3ad99dd01ed465140a1a229 Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 9 Jun 2023 11:23:29 +0200 Subject: [PATCH 01/18] Parse switches output and component from CLI - parse output path and component to run as switch -o and --component --- lib/cli.ex | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 0a0f638..1e3b05d 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -1,7 +1,28 @@ defmodule Onigumo.CLI do - def main([component]) do - module = Module.safe_concat("Onigumo", component) - root_path = File.cwd!() - module.main(root_path) + def main(argv) do + {parsed_switches, _, _} = + argv + |> parse_argv + + module = + parsed_switches + |> Keyword.get(:component, "Downloader") + |> safe_concat() + + parsed_switches + |> Keyword.get(:output, File.cwd!()) + |> module.main() + end + + defp parse_argv(argv) do + argv + |> OptionParser.parse( + aliases: [o: :output], + strict: [component: :string, output: :string] + ) + end + + defp safe_concat(component) do + Module.safe_concat("Onigumo", component) end end From bec0fe63c882c42ea7780c1bb8e246b2a9666e3e Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 9 Jun 2023 11:27:19 +0200 Subject: [PATCH 02/18] Fix formatting --- lib/cli.ex | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 1e3b05d..0cdfe76 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -15,11 +15,11 @@ defmodule Onigumo.CLI do end defp parse_argv(argv) do - argv - |> OptionParser.parse( - aliases: [o: :output], - strict: [component: :string, output: :string] - ) + argv + |> OptionParser.parse( + aliases: [o: :output], + strict: [component: :string, output: :string] + ) end defp safe_concat(component) do From 56acd648a98d805197cdd6f4c0d593ff12e724c8 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sun, 15 Oct 2023 19:01:35 +0200 Subject: [PATCH 03/18] Parse working_dir switch --- lib/cli.ex | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 0ea12a9..ad9fde3 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -1,8 +1,10 @@ defmodule Onigumo.CLI do def main(argv) do - {[], [component]} = OptionParser.parse!(argv, strict: []) + {parsed_switches, [component]} = OptionParser.parse!(argv, strict: [working_dir: :string]) module = Module.safe_concat("Onigumo", component) - root_path = File.cwd!() - module.main(root_path) + + parsed_switches + |> Keyword.get(:working_dir, File.cwd!()) + |> module.main end end From 3b30377856dce84f6891e070b16d075701d155a8 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 24 Feb 2024 22:13:24 +0100 Subject: [PATCH 04/18] Parse a new switch --working-dir from CLI --working-dir is switch which allow user use different working directory than only current working directory --- lib/cli.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 53e2f6f..92af5fc 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -4,10 +4,10 @@ defmodule Onigumo.CLI do } def main(argv) do - case OptionParser.parse(argv, strict: []) do - {[], [component], []} -> + case OptionParser.parse(argv, strict: [working_dir: :string]) do + {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) - root_path = File.cwd!() + root_path = Keyword.get(parsed_switches, :working_dir, File.cwd!()) module.main(root_path) _ -> From b0a4b892157c5d255621926742c54cb79e310793 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 16 Mar 2024 15:39:15 +0100 Subject: [PATCH 05/18] Rename variable 'root_path' to 'working_dir' - working_dir is more precise naming for the intend purpose, - working_dir is also name of the switch we use on CLI --- lib/cli.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 92af5fc..21e6c8b 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -7,8 +7,8 @@ defmodule Onigumo.CLI do case OptionParser.parse(argv, strict: [working_dir: :string]) do {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) - root_path = Keyword.get(parsed_switches, :working_dir, File.cwd!()) - module.main(root_path) + working_dir = Keyword.get(parsed_switches, :working_dir, File.cwd!()) + module.main(working_dir) _ -> usage_message() From 4a11e4ecee3e7858bfa208450006583b3ca2cecf Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 16 Mar 2024 16:04:03 +0100 Subject: [PATCH 06/18] Update help message with usage of --working-dir --- lib/cli.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cli.ex b/lib/cli.ex index 21e6c8b..245c162 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -19,11 +19,14 @@ defmodule Onigumo.CLI do components = Enum.join(Map.keys(@components), ", ") IO.puts(""" - Usage: onigumo [COMPONENT] + Usage: onigumo [OPTION]... [COMPONENT] Simple program that retrieves HTTP web content as structured data. COMPONENT\tOnigumo component to run, available: #{components} + + OPTIONS: + --working-dir=DIR\tChange to before processing remaining files """) end end From 02c0b2cf32114aec65ee1d7b91eccb4c609466c8 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 16 Mar 2024 16:21:19 +0100 Subject: [PATCH 07/18] Add shorthand version for switch --working-dir --- lib/cli.ex | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 245c162..683940b 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -4,7 +4,11 @@ defmodule Onigumo.CLI do } def main(argv) do - case OptionParser.parse(argv, strict: [working_dir: :string]) do + case OptionParser.parse( + argv, + aliases: [C: :working_dir], + strict: [working_dir: :string] + ) do {parsed_switches, [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) working_dir = Keyword.get(parsed_switches, :working_dir, File.cwd!()) @@ -26,7 +30,7 @@ defmodule Onigumo.CLI do COMPONENT\tOnigumo component to run, available: #{components} OPTIONS: - --working-dir=DIR\tChange to before processing remaining files + -C, --working-dir=DIR\tChange to before processing remaining files """) end end From 945287220f3414b629814288220be288e5b5a615 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 6 Apr 2024 22:25:02 +0200 Subject: [PATCH 08/18] Add tests for working_dir switch usage --- test/onigumo_cli_test.exs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index fd9dd68..9d1cfb8 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -13,6 +13,11 @@ defmodule OnigumoCLITest do "uploader" ] + @invalid_switches [ + "--invalid", + "-c" + ] + describe("Onigumo.CLI.main/1") do for argument <- @invalid_arguments do test("run CLI with invalid argument #{inspect(argument)}") do @@ -28,8 +33,10 @@ defmodule OnigumoCLITest do assert usage_message_printed?(fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end) end - test("run CLI with invalid switch") do - assert usage_message_printed?(fn -> Onigumo.CLI.main(["--invalid"]) end) + for switch <- @invalid_switches do + test("run CLI with invalid switch #{switch}") do + assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switch)]) end) + end end @tag :tmp_dir @@ -40,6 +47,20 @@ defmodule OnigumoCLITest do assert Onigumo.CLI.main(["downloader"]) == tmp_dir end + @tag :tmp_dir + test("run CLI 'downloader' with '--working-dir' switch", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) + + assert Onigumo.CLI.main(["downloader", "--working-dir", tmp_dir]) == tmp_dir + end + + @tag :tmp_dir + test("run CLI 'downloader' with '-C' switch", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) + + assert Onigumo.CLI.main(["downloader", "-C", tmp_dir]) == tmp_dir + end + defp usage_message_printed?(function) do output = capture_io(function) String.starts_with?(output, "Usage: onigumo ") From 0988b1305028d223471de2e09264c8e3e6217690 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 15:51:33 +0200 Subject: [PATCH 09/18] Improve usage message for working_dir switch --- lib/cli.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli.ex b/lib/cli.ex index c48e588..cdc5a0a 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -30,7 +30,7 @@ defmodule Onigumo.CLI do COMPONENT\tOnigumo component to run, available: #{components} OPTIONS: - -C, --working-dir=DIR\tChange to before processing remaining files + -C, --working-dir \tChange to before running """) end end From a5e9edc1a14a50199756f3da7963176f518bf2cc Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:18:54 +0200 Subject: [PATCH 10/18] Parametrize tests for working_dir switch with long and short --- test/onigumo_cli_test.exs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index f6686ee..e1e5b88 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -13,6 +13,11 @@ defmodule OnigumoCLITest do "-c" ] + @working_dir_switches [ + "--working-dir", + "-C" + ] + describe("Onigumo.CLI.main/1") do for argument <- @invalid_arguments do test("run CLI with invalid argument #{inspect(argument)}") do @@ -42,18 +47,14 @@ defmodule OnigumoCLITest do assert Onigumo.CLI.main(["downloader"]) == tmp_dir end - @tag :tmp_dir - test("run CLI 'downloader' with '--working-dir' switch", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) - assert Onigumo.CLI.main(["downloader", "--working-dir", tmp_dir]) == tmp_dir - end + for switch <- @working_dir_switches do + @tag :tmp_dir + test("run CLI 'downloader' with #{inspect(switch)} switch", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) - @tag :tmp_dir - test("run CLI 'downloader' with '-C' switch", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) - - assert Onigumo.CLI.main(["downloader", "-C", tmp_dir]) == tmp_dir + assert Onigumo.CLI.main(["downloader", unquote(switch), tmp_dir]) == tmp_dir + end end defp usage_message_printed?(function) do From 9fbdbafda1b845f56d04126ad2129b043f7bf6b6 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:20:14 +0200 Subject: [PATCH 11/18] Be consistent with naming of variables in tests with the code --- test/onigumo_cli_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index e1e5b88..1467d36 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -41,7 +41,7 @@ defmodule OnigumoCLITest do @tag :tmp_dir test("run CLI with 'downloader' argument passing cwd", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) + expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) File.cd(tmp_dir) assert Onigumo.CLI.main(["downloader"]) == tmp_dir @@ -51,7 +51,7 @@ defmodule OnigumoCLITest do for switch <- @working_dir_switches do @tag :tmp_dir test("run CLI 'downloader' with #{inspect(switch)} switch", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end) + expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) assert Onigumo.CLI.main(["downloader", unquote(switch), tmp_dir]) == tmp_dir end From c73a42fa5d36cb39890a58850f23eeb207237ca8 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:21:58 +0200 Subject: [PATCH 12/18] Use inspect to print string in all descriptions of tests --- 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 1467d36..5ae87cb 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -34,7 +34,7 @@ defmodule OnigumoCLITest do end for switch <- @invalid_switches do - test("run CLI with invalid switch #{switch}") do + test("run CLI with invalid switch #{inspect(switch)}") do assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switch)]) end) end end From 47e06ed305aef9a2270ff3d3ba81cd067c1670ef Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:35:06 +0200 Subject: [PATCH 13/18] Test switch --working-dir without value --- 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 5ae87cb..3b9dbd4 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -57,6 +57,12 @@ defmodule OnigumoCLITest do end end + test("run CLI 'downloader' with '--working-dir' without any value") do + assert usage_message_printed?( + fn -> Onigumo.CLI.main(["downloader", "--working-dir"]) end + ) + end + defp usage_message_printed?(function) do output = capture_io(function) String.starts_with?(output, "Usage: onigumo ") From b3c5bf34346d875e3c2ed5dee116fbe3154bb338 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:51:14 +0200 Subject: [PATCH 14/18] Test switch --working-dir with value '.' --- test/onigumo_cli_test.exs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 3b9dbd4..2b00099 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -61,6 +61,13 @@ defmodule OnigumoCLITest do assert usage_message_printed?( fn -> Onigumo.CLI.main(["downloader", "--working-dir"]) end ) + + @tag :tmp_dir + test("run CLI 'downloader' with '--working-dir' and value '.'", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) + + File.cd(tmp_dir) + assert Onigumo.CLI.main(["downloader", "--working-dir", "."]) == "." end defp usage_message_printed?(function) do From f883c28bb7929f26325d83ee5015f0ad5331c40d Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 20 Apr 2024 16:51:43 +0200 Subject: [PATCH 15/18] Fix formatting --- test/onigumo_cli_test.exs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 2b00099..67e4fee 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -47,7 +47,6 @@ defmodule OnigumoCLITest do assert Onigumo.CLI.main(["downloader"]) == tmp_dir end - for switch <- @working_dir_switches do @tag :tmp_dir test("run CLI 'downloader' with #{inspect(switch)} switch", %{tmp_dir: tmp_dir}) do @@ -58,9 +57,8 @@ defmodule OnigumoCLITest do end test("run CLI 'downloader' with '--working-dir' without any value") do - assert usage_message_printed?( - fn -> Onigumo.CLI.main(["downloader", "--working-dir"]) end - ) + assert usage_message_printed?(fn -> Onigumo.CLI.main(["downloader", "--working-dir"]) end) + end @tag :tmp_dir test("run CLI 'downloader' with '--working-dir' and value '.'", %{tmp_dir: tmp_dir}) do From e57bfa706f83a16f119a86c0209d393fc53d1756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20=C5=A0troch?= <45515588+nappex@users.noreply.github.com> Date: Fri, 10 May 2024 19:07:23 +0200 Subject: [PATCH 16/18] Update CLI help message for working dir Co-authored-by: Glutexo --- lib/cli.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli.ex b/lib/cli.ex index cdc5a0a..6a76a11 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -30,7 +30,7 @@ defmodule Onigumo.CLI do COMPONENT\tOnigumo component to run, available: #{components} OPTIONS: - -C, --working-dir \tChange to before running + -C, --working-dir \tChange working dir to before running """) end end From e227fb2855c3da61260660c9fc5e83de213755dd Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 10 May 2024 19:21:36 +0200 Subject: [PATCH 17/18] Parametrize all tests with working-dir switch --- test/onigumo_cli_test.exs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index 67e4fee..d500cff 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -54,18 +54,18 @@ defmodule OnigumoCLITest do assert Onigumo.CLI.main(["downloader", unquote(switch), tmp_dir]) == tmp_dir end - end - test("run CLI 'downloader' with '--working-dir' without any value") do - assert usage_message_printed?(fn -> Onigumo.CLI.main(["downloader", "--working-dir"]) end) - end + test("run CLI 'downloader' with #{inspect(switch)} without any value") do + assert usage_message_printed?(fn -> Onigumo.CLI.main(["downloader", unquote(switch)]) end) + end - @tag :tmp_dir - test("run CLI 'downloader' with '--working-dir' and value '.'", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) + @tag :tmp_dir + test("run CLI 'downloader' with #{inspect(switch)} and value '.'", %{tmp_dir: tmp_dir}) do + expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) - File.cd(tmp_dir) - assert Onigumo.CLI.main(["downloader", "--working-dir", "."]) == "." + File.cd(tmp_dir) + assert Onigumo.CLI.main(["downloader", unquote(switch), "."]) == "." + end end defp usage_message_printed?(function) do From 4120a34026927c9752a771ae98250615ace61734 Mon Sep 17 00:00:00 2001 From: dstroch Date: Fri, 10 May 2024 19:26:22 +0200 Subject: [PATCH 18/18] Remove test with no additional value --- test/onigumo_cli_test.exs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test/onigumo_cli_test.exs b/test/onigumo_cli_test.exs index d500cff..c0d51ba 100644 --- a/test/onigumo_cli_test.exs +++ b/test/onigumo_cli_test.exs @@ -58,14 +58,6 @@ defmodule OnigumoCLITest do test("run CLI 'downloader' with #{inspect(switch)} without any value") do assert usage_message_printed?(fn -> Onigumo.CLI.main(["downloader", unquote(switch)]) end) end - - @tag :tmp_dir - test("run CLI 'downloader' with #{inspect(switch)} and value '.'", %{tmp_dir: tmp_dir}) do - expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end) - - File.cd(tmp_dir) - assert Onigumo.CLI.main(["downloader", unquote(switch), "."]) == "." - end end defp usage_message_printed?(function) do