parent
ad7be8bc62
commit
8f002646a2
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue