Add basic CLI
This commit is contained in:
parent
2acc0eeee1
commit
e0330fdec0
|
@ -0,0 +1,50 @@
|
|||
defmodule AmazonHistory.CLI do
|
||||
@moduledoc """
|
||||
Handle the command-line parsing for the amazon_history tool
|
||||
"""
|
||||
|
||||
def run(argv) do
|
||||
argv
|
||||
|> parse_args
|
||||
|> process
|
||||
end
|
||||
|
||||
def process(username: username, password: password) do
|
||||
IO.puts("Will continue with username: #{username}, password: #{password}")
|
||||
end
|
||||
|
||||
def process(_) do
|
||||
IO.puts("Usage: --username <username> --password <password>")
|
||||
end
|
||||
|
||||
@doc """
|
||||
Options:
|
||||
-u/--username: Amazon username
|
||||
-p/--password: Amazon password
|
||||
"""
|
||||
def parse_args(argv) do
|
||||
OptionParser.parse(
|
||||
argv,
|
||||
switches: [
|
||||
help: :boolean,
|
||||
username: :string,
|
||||
password: :string
|
||||
],
|
||||
aliases: [
|
||||
h: :help,
|
||||
u: :username,
|
||||
p: :password
|
||||
]
|
||||
)
|
||||
|> elem(0)
|
||||
|> args_to_internal_representation
|
||||
end
|
||||
|
||||
defp args_to_internal_representation(username: username, password: password) do
|
||||
[username: username, password: password]
|
||||
end
|
||||
|
||||
defp args_to_internal_representation(_) do
|
||||
:help
|
||||
end
|
||||
end
|
|
@ -0,0 +1,31 @@
|
|||
defmodule CliTest do
|
||||
use ExUnit.Case
|
||||
|
||||
describe "parse_args/1" do
|
||||
import AmazonHistory.CLI, only: [parse_args: 1]
|
||||
|
||||
test ":help returned by passing -h and --help options" do
|
||||
assert parse_args(["-h"]) == :help
|
||||
assert parse_args(["--help"]) == :help
|
||||
end
|
||||
|
||||
test ":help returned by passing only a username" do
|
||||
assert parse_args(["-u", "rob"]) == :help
|
||||
assert parse_args(["--username", "rob"]) == :help
|
||||
end
|
||||
|
||||
test ":help returned by passing only a password" do
|
||||
assert parse_args(["-p", "hackme"]) == :help
|
||||
assert parse_args(["--password", "hackme"]) == :help
|
||||
end
|
||||
|
||||
test "arguments returned by passing valid parameters" do
|
||||
assert parse_args(["-u", "rob", "-p", "hackme"]) == [username: "rob", password: "hackme"]
|
||||
|
||||
assert parse_args(["--username", "rob", "--password", "hackme"]) == [
|
||||
username: "rob",
|
||||
password: "hackme"
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue