Created
March 1, 2016 07:38
-
-
Save s3itz/3be28d14de0093d39fe8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Issues.CLI do | |
@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. | |
""" | |
@default_count 4 | |
def run(argv) do | |
argv | |
|> parse_args | |
|> process | |
end | |
@doc """ | |
`argv` can be -h or --help, which returns :help. | |
Otherwise, it is a GitHub username, project name, and optionally the number | |
of entries to format. | |
Return a tuple of `{user, project, count}`, or `:help` if help was given. | |
""" | |
def parse_args(argv) do | |
parse = OptionParser.parse(argv, switches: [help: :booleaan], | |
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 | |
def process(:help) do | |
IO.puts """ | |
usage: issues <user> <project> [ count | #{@default_count} ] ] | |
""" | |
System.halt(0) | |
end | |
def ({user, project, _count}) do | |
Issues.GitHubIssues.fetch(user, project) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment