diff --git a/lib/elixir_rss/cli.ex b/lib/elixir_rss/cli.ex new file mode 100644 index 0000000..0cc7570 --- /dev/null +++ b/lib/elixir_rss/cli.ex @@ -0,0 +1,43 @@ +defmodule ElixirRss.CLI do + @default_host "0.0.0.0" + @default_port 8080 + @default_basepath "/" + + def run(argv) do + parse_args(argv) + |> process() + end + + def parse_args(argv) do + OptionParser.parse( + argv, + strict: [help: :boolean, + host: :string, + port: :integer, + path: :string], + aliases: [h: :help]) + |> elem(0) + |> switches_to_internal() + end + + defp switches_to_internal(kwargs) do + if Keyword.get(kwargs, :help) do + :help + else + {Keyword.get(kwargs, :host, @default_host), + Keyword.get(kwargs, :port, @default_port), + Keyword.get(kwargs, :path, @default_basepath)} + end + end + + defp process(:help) do + IO.puts(""" + usage: elixir_rss [--host HOST] [--port PORT] [--path BASEPATH] + """) + System.halt(0) + end + + defp process({host, port, path}) do + IO.puts("Host: #{host}, Port: #{port}, Basepath: #{path}") + end +end diff --git a/lib/fetch_rss.ex b/lib/elixir_rss/http.ex similarity index 90% rename from lib/fetch_rss.ex rename to lib/elixir_rss/http.ex index a0a4b26..0bcc0f8 100644 --- a/lib/fetch_rss.ex +++ b/lib/elixir_rss/http.ex @@ -1,4 +1,4 @@ -defmodule ElixirRss.Fetch do +defmodule ElixirRss.Http do def fetch(url) do url |> HTTPoison.get() diff --git a/test/cli_test.exs b/test/cli_test.exs new file mode 100644 index 0000000..4e15a1d --- /dev/null +++ b/test/cli_test.exs @@ -0,0 +1,31 @@ +defmodule CliTest do + use ExUnit.Case + doctest ElixirRss + + import ElixirRss.CLI, only: [parse_args: 1] + + test ":help returned for -h, --help, and default" do + assert parse_args(["-h"]) == :help + assert parse_args(["--help"]) == :help + assert parse_args(["other", "-h", "stuff"]) == :help + assert parse_args(["other", "--help", "stuff"]) == :help + end + + test "all switches given" do + assert parse_args(["--host", "127.0.0.1", "--port", "80", "--path", "/path"]) == {"127.0.0.1", 80, "/path"} + end + + test "defaults returned for missing args" do + assert parse_args([]) == {"0.0.0.0", 8080, "/"} + assert parse_args(["--host", "127.0.0.1"]) == {"127.0.0.1", 8080, "/"} + assert parse_args(["--port", "80"]) == {"0.0.0.0", 80, "/"} + assert parse_args(["--path", "/path"]) == {"0.0.0.0", 8080, "/path"} + assert parse_args(["--host", "127.0.0.1", "--port", "80"]) == {"127.0.0.1", 80, "/"} + assert parse_args(["--host", "127.0.0.1", "--path", "/path"]) == {"127.0.0.1", 8080, "/path"} + assert parse_args(["--port", "80", "--path", "/path"]) == {"0.0.0.0", 80, "/path"} + end + + test "out of order switches" do + assert parse_args(["--port", "80", "--host", "127.0.0.1", "--path", "/path"]) == {"127.0.0.1", 80, "/path"} + end +end diff --git a/test/fetch_rss_test.exs b/test/fetch_rss_test.exs index 056f814..bf5599a 100644 --- a/test/fetch_rss_test.exs +++ b/test/fetch_rss_test.exs @@ -1,8 +1,8 @@ -defmodule ElixirRss.FetchTest do +defmodule HttpTest do use ExUnit.Case - doctest ElixirRss.Fetch + doctest ElixirRss test "fetch url" do - assert ElixirRss.Fetch.fetch("https://shillerben.com") |> elem(0) == :ok + assert ElixirRss.Http.fetch("https://shillerben.com") |> elem(0) == :ok end end