Skip to content

Commit 0d890b9

Browse files
authored
Merge pull request #195 from nappex/parse-working-dir
Parse working_dir switch
2 parents 96c2bd6 + 4120a34 commit 0d890b9

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

lib/cli.ex

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ defmodule Onigumo.CLI do
44
}
55

66
def main(argv) do
7-
case OptionParser.parse(argv, strict: []) do
8-
{[], [component], []} ->
7+
case OptionParser.parse(
8+
argv,
9+
aliases: [C: :working_dir],
10+
strict: [working_dir: :string]
11+
) do
12+
{parsed_switches, [component], []} ->
913
{:ok, module} = Map.fetch(@components, String.to_atom(component))
10-
root_path = File.cwd!()
11-
module.main(root_path)
14+
working_dir = Keyword.get(parsed_switches, :working_dir, File.cwd!())
15+
module.main(working_dir)
1216

1317
_ ->
1418
usage_message()
@@ -19,11 +23,14 @@ defmodule Onigumo.CLI do
1923
components = Enum.join(Map.keys(@components), ", ")
2024

2125
IO.puts("""
22-
Usage: onigumo [COMPONENT]
26+
Usage: onigumo [OPTION]... [COMPONENT]
2327
2428
Simple program that retrieves HTTP web content as structured data.
2529
2630
COMPONENT\tOnigumo component to run, available: #{components}
31+
32+
OPTIONS:
33+
-C, --working-dir <dir>\tChange working dir to <dir> before running
2734
""")
2835
end
2936
end

test/onigumo_cli_test.exs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ defmodule OnigumoCLITest do
88
"uploader"
99
]
1010

11+
@invalid_switches [
12+
"--invalid",
13+
"-c"
14+
]
15+
16+
@working_dir_switches [
17+
"--working-dir",
18+
"-C"
19+
]
20+
1121
describe("Onigumo.CLI.main/1") do
1222
for argument <- @invalid_arguments do
1323
test("run CLI with invalid argument #{inspect(argument)}") do
@@ -23,18 +33,33 @@ defmodule OnigumoCLITest do
2333
assert usage_message_printed?(fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end)
2434
end
2535

26-
test("run CLI with invalid switch") do
27-
assert usage_message_printed?(fn -> Onigumo.CLI.main(["--invalid"]) end)
36+
for switch <- @invalid_switches do
37+
test("run CLI with invalid switch #{inspect(switch)}") do
38+
assert usage_message_printed?(fn -> Onigumo.CLI.main([unquote(switch)]) end)
39+
end
2840
end
2941

3042
@tag :tmp_dir
3143
test("run CLI with 'downloader' argument passing cwd", %{tmp_dir: tmp_dir}) do
32-
expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end)
44+
expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end)
3345

3446
File.cd(tmp_dir)
3547
assert Onigumo.CLI.main(["downloader"]) == tmp_dir
3648
end
3749

50+
for switch <- @working_dir_switches do
51+
@tag :tmp_dir
52+
test("run CLI 'downloader' with #{inspect(switch)} switch", %{tmp_dir: tmp_dir}) do
53+
expect(OnigumoDownloaderMock, :main, fn working_dir -> working_dir end)
54+
55+
assert Onigumo.CLI.main(["downloader", unquote(switch), tmp_dir]) == tmp_dir
56+
end
57+
58+
test("run CLI 'downloader' with #{inspect(switch)} without any value") do
59+
assert usage_message_printed?(fn -> Onigumo.CLI.main(["downloader", unquote(switch)]) end)
60+
end
61+
end
62+
3863
defp usage_message_printed?(function) do
3964
output = capture_io(function)
4065
String.starts_with?(output, "Usage: onigumo ")

0 commit comments

Comments
 (0)