Add option parser and tests for it

TESTS!!!!
This commit is contained in:
Nathan Mattes 2021-11-30 21:48:17 +01:00
parent ad7be8bc62
commit 8f002646a2
2 changed files with 53 additions and 0 deletions

34
issues/lib/issues/cli.ex Normal file
View File

@ -0,0 +1,34 @@
defmodule Issues.CLI do
@default_count 4
@moduledoc """
Handle the command line parsing and the dispatch to the various functions that end up
generating a table of the last _n_ issues in a github project.
"""
def run(argv) do
parse_args(argv)
end
def parse_args(argv) do
parse =
OptionParser.parse(argv,
switches: [help: :boolean],
aliases: [h: :help]
)
case parse do
{[help: true], _, _} ->
:help
{_, [user, project, count], _} ->
{user, project, String.to_integer(count)}
{_, [user, project], _} ->
{user, project, @default_count}
_ ->
:help
end
end
end

19
issues/test/cli_test.exs Normal file
View File

@ -0,0 +1,19 @@
defmodule CliTest do
use ExUnit.Case
doctest Issues
import Issues.CLI, only: [parse_args: 1]
test ":help returned by option parsing the -h and --help options" do
assert parse_args(["-h", "anything"]) == :help
assert parse_args(["--help", "anything"]) == :help
end
test "three values returned if three given" do
assert parse_args(["user", "project", "99"]) == {"user", "project", 99}
end
test "two values and default count returned if two values given" do
assert parse_args(["user", "project"]) == {"user", "project", 4}
end
end