Extract start function for options
This commit is contained in:
@@ -7,7 +7,7 @@ defmodule Mix.Tasks.Meditate do
|
|||||||
Code.compiler_options(ignore_module_conflict: true)
|
Code.compiler_options(ignore_module_conflict: true)
|
||||||
Watcher.start
|
Watcher.start
|
||||||
|
|
||||||
Options.parse(args)
|
Options.start(args)
|
||||||
Runner.run
|
Runner.run
|
||||||
|
|
||||||
:timer.sleep(:infinity)
|
:timer.sleep(:infinity)
|
||||||
|
|||||||
@@ -3,12 +3,8 @@ defmodule Options do
|
|||||||
clear_screen: false
|
clear_screen: false
|
||||||
}
|
}
|
||||||
|
|
||||||
def parse(args) do
|
def start(args) do
|
||||||
options = Enum.reduce(args, @defaults, fn(arg, acc) ->
|
Agent.start_link(fn -> parse(args) end, name: __MODULE__)
|
||||||
Map.merge(acc, parse_argument(arg))
|
|
||||||
end)
|
|
||||||
|
|
||||||
Agent.start_link(fn -> options end, name: __MODULE__)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_screen? do
|
def clear_screen? do
|
||||||
@@ -17,6 +13,12 @@ defmodule Options do
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp parse(args) do
|
||||||
|
Enum.reduce(args, @defaults, fn(arg, acc) ->
|
||||||
|
Map.merge(acc, parse_argument(arg))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
def parse_argument("--clear-screen"), do: %{ clear_screen: true}
|
def parse_argument("--clear-screen"), do: %{ clear_screen: true}
|
||||||
def parse_argument(_), do: %{}
|
def parse_argument(_), do: %{}
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,17 +2,17 @@ defmodule OptionTest do
|
|||||||
use ExUnit.Case, async: true
|
use ExUnit.Case, async: true
|
||||||
|
|
||||||
test "has default options" do
|
test "has default options" do
|
||||||
Options.parse([])
|
Options.start([])
|
||||||
refute Options.clear_screen?
|
refute Options.clear_screen?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "override clearing of screen" do
|
test "override clearing of screen" do
|
||||||
Options.parse(["--clear-screen"])
|
Options.start(["--clear-screen"])
|
||||||
assert Options.clear_screen?
|
assert Options.clear_screen?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "ignores unknown options" do
|
test "ignores unknown options" do
|
||||||
options = Options.parse(["--foo"])
|
Options.start(["--foo"])
|
||||||
refute Options.clear_screen?
|
refute Options.clear_screen?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user