Switch over to Elixir option parsing.

This commit is contained in:
Nathan Walker
2017-04-26 21:51:26 -05:00
parent 372d7f70ac
commit b32e82b19b
5 changed files with 14 additions and 39 deletions

View File

@@ -1,6 +1,5 @@
defmodule ElixirKoans do
use Application
alias Options
def start(_type, _args) do
import Supervisor.Spec

View File

@@ -1,6 +1,5 @@
defmodule Mix.Tasks.Meditate do
use Mix.Task
alias Options
@shortdoc "Start the koans"
@@ -8,26 +7,34 @@ defmodule Mix.Tasks.Meditate do
Application.ensure_all_started(:elixir_koans)
Code.compiler_options(ignore_module_conflict: true)
Options.start(args)
{parsed, _, _} = OptionParser.parse(args)
if Keyword.has_key?(parsed, :no_clear_screen) do
Display.disable_clear()
end
modules = Options.initial_koan
modules = parsed
|> initial_module
|> ok?
|> Runner.modules_to_run
Tracker.set_total(modules)
Tracker.notify_on_complete(self())
set_clear_screen(parsed)
Runner.run(modules)
Tracker.wait_until_complete()
Display.congratulate()
end
defp initial_module(parsed) do
name = Keyword.get(parsed, :koan, "Equalities")
String.to_atom("Elixir."<> name)
end
defp set_clear_screen(parsed) do
if Keyword.has_key?(parsed, :no_clear_screen) do
Display.disable_clear()
end
end
defp ok?(koan) do
if Runner.koan?(koan) do
koan

View File

@@ -1,22 +0,0 @@
defmodule Options do
@defaults %{
initial_koan: Equalities,
}
def start(args) do
Agent.start_link(fn -> parse(args) end, name: __MODULE__)
end
def initial_koan() do
Agent.get(__MODULE__, &Map.fetch!(&1, :initial_koan))
end
defp parse(args) do
Enum.reduce(args, @defaults, fn(arg, acc) ->
Map.merge(acc, parse_argument(arg))
end)
end
def parse_argument("--koan="<>module), do: %{ initial_koan: String.to_atom("Elixir."<> module)}
def parse_argument(_), do: %{}
end

View File

@@ -25,7 +25,6 @@ defmodule Runner do
String.to_integer(number)
end
def modules_to_run, do: Options.initial_koan |> modules_to_run
def modules_to_run(start_module), do: Enum.drop_while(modules(), &(&1 != start_module))
def start_link do

View File

@@ -1,8 +0,0 @@
defmodule OptionTest do
use ExUnit.Case, async: true
test "can target specifc koans" do
Options.start(["--koan=Strings"])
assert Options.initial_koan() == Strings
end
end