Skip to content

Commit

Permalink
Rebranding to Commando
Browse files Browse the repository at this point in the history
  • Loading branch information
svetob committed Jun 4, 2017
1 parent f38d9bd commit 7b809ea
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 84 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[![Build Status](https://travis-ci.org/svetob/commander.svg?branch=master)](https://travis-ci.org/svetob/commander)
[![Build Status](https://travis-ci.org/svetob/commando.svg?branch=master)](https://travis-ci.org/svetob/commando)

# Commander
# Commando

__Commander__ is a command line parser which uses OptionParser behind-the-hood
__Commando__ is a command line parser which uses OptionParser behind-the-hood
and extends it with:

- Simple and informative help messages
Expand All @@ -12,11 +12,11 @@ and extends it with:

## Usage

Add `commander` as a dependency to your `mix.exs` file:
Add `commando` as a dependency to your `mix.exs` file:

```
defp deps do
[{:commander, "~> 0.1"}]
[{:commando, "~> 0.1"}]
end
```

Expand All @@ -26,19 +26,19 @@ Then run `mix deps.get` to download it.

`example.exs`:
```Elixir
c = Commander.create("example app", "An example application", "mix run example.exs")
|> Commander.with_help()
|> Commander.with_switch(:port, :integer, "HTTP port", required: true, alias: :p)
|> Commander.with_switch(:data_path, :string, "Data path", default: "data/", alias: :d)
c = Commando.create("example app", "An example application", "mix run example.exs")
|> Commando.with_help()
|> Commando.with_switch(:port, :integer, "HTTP port", required: true, alias: :p)
|> Commando.with_switch(:data_path, :string, "Data path", default: "data/", alias: :d)

case Commander.parse(c, System.argv()) do
case Commando.parse(c, System.argv()) do
{:ok, result} ->
IO.inspect result
{:help, message} ->
IO.puts message
{:error, reason} ->
IO.puts reason
IO.puts Commander.help_message(c)
IO.puts Commando.help_message(c)
end
```

Expand Down
4 changes: 2 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use Mix.Config

# You can configure for your application as:
#
# config :commander, key: :value
# config :commando, key: :value
#
# And access this configuration in your application as:
#
# Application.get_env(:commander, :key)
# Application.get_env(:commando, :key)
#
# Or configure a 3rd-party app:
#
Expand Down
110 changes: 55 additions & 55 deletions lib/commander.ex → lib/commando.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Commander do
alias Commander.State
alias Commander.Help
defmodule Commando do
alias Commando.State
alias Commando.Help

@moduledoc """
Command line parser with default values, useful help messages, and other features.
Expand All @@ -18,17 +18,17 @@ defmodule Commander do
@type parse_result :: [{atom(), any()}]

@doc ~S"""
Creates a new Commander instance.
Creates a new Commando instance.
## Examples
iex> Commander.create("app").app_name
iex> Commando.create("app").app_name
"app"
iex> Commander.create("app", "Doc test app").app_description
iex> Commando.create("app", "Doc test app").app_description
"Doc test app"
iex> Commander.create("app", "Doc test app", "mix run").example
iex> Commando.create("app", "Doc test app", "mix run").example
"mix run"
"""
@spec create(String.t, String.t, String.t) :: State.t
Expand All @@ -39,28 +39,28 @@ defmodule Commander do
end

@doc """
Add a standardized help-message switch (--help, -h) to the Commander instance.
Add a standardized help-message switch (--help, -h) to the Commando instance.
## Usage
The application should check the result of `Commander.parse/2` to verify if
this switch is true. If so, it should show the `Commander.help_message/1` and
The application should check the result of `Commando.parse/2` to verify if
this switch is true. If so, it should show the `Commando.help_message/1` and
exit.
"""
@spec with_help(State.t) :: State.t
def with_help(commander) do
commander |> with_switch(:help, :boolean, "Print help message", alias: :h)
def with_help(commando) do
commando |> with_switch(:help, :boolean, "Print help message", alias: :h)
end

@doc """
Add a switch to a Commander instance.
Add a switch to a Commando instance.
Returns a new Commander instance containing the switch.
Returns a new Commando instance containing the switch.
## Description
The description will be shown for the switch in the help message generated by
`Commander.help_message/1`.
`Commando.help_message/1`.
## Switch Types
Expand All @@ -86,22 +86,22 @@ defmodule Commander do
* `default: any` - If switch is not specified, it will recieve this default value.
* `alias: atom` - An alias for this switch. e.g. for `:data` you might
pass `alias: :d`, you can then on the command line use `--data` or `-d`.
* `required: boolean` - If true, `Commander.parse/2` will return an error if
* `required: boolean` - If true, `Commando.parse/2` will return an error if
this switch is not present in `args`.
## Examples
iex> Commander.create("test_app", "Test app", "mix run") |>
...> Commander.with_switch(:path, :string, "Path", required: true, alias: :p, default: "path/")
%Commander.State{aliases: [p: :path], app_description: "Test app",
iex> Commando.create("test_app", "Test app", "mix run") |>
...> Commando.with_switch(:path, :string, "Path", required: true, alias: :p, default: "path/")
%Commando.State{aliases: [p: :path], app_description: "Test app",
app_name: "test_app", defaults: [path: "path/"],
descriptions: [path: "Path"], example: "mix run", required: [:path],
switches: [path: :string]}
"""
@spec with_switch(State.t, atom(), State.switch_type, String.t, conf_list) :: State.t
def with_switch(commander, switch, type, description, conf \\ []) do
commander
def with_switch(commando, switch, type, description, conf \\ []) do
commando
|> State.add_switch(switch, type)
|> State.add_description(switch, description)
|> add_configurations(switch, conf)
Expand All @@ -112,7 +112,7 @@ defmodule Commander do
Returns one of:
* `{:help, message}` - If you added the help swith using `Commander.with_help/1`
* `{:help, message}` - If you added the help swith using `Commando.with_help/1`
and `--help` or `-h` switches were present. `message` contains the formatted
help message to show.
* `{:ok, result}` - If command line args were parsed successfully and all
Expand All @@ -124,43 +124,43 @@ defmodule Commander do
## Examples
iex> Commander.create("app") |> Commander.with_switch(:path, :string, "Path") |> Commander.parse(["--path", "abc"])
iex> Commando.create("app") |> Commando.with_switch(:path, :string, "Path") |> Commando.parse(["--path", "abc"])
{:ok, [path: "abc"]}
iex> import Commander
iex> import Commando
iex> create("app") |> with_switch(:path, :string, "Path", alias: :p) |> parse(["-p", "abc"])
{:ok, [path: "abc"]}
iex> import Commander
iex> import Commando
iex> create("app") |> parse(["--path", "abc"])
{:error, "Unknown options: --path"}
iex> import Commander
iex> import Commando
iex> create("app") |> with_switch(:foo, :boolean, "") |> parse(["--foo"])
{:ok, [foo: true]}
iex> import Commander
iex> import Commando
iex> create("app") |> with_switch(:foo, :count, "", alias: :f) |> parse(["--foo", "-f", "-f"])
{:ok, [foo: 3]}
iex> import Commander
iex> import Commando
iex> create("app") |> with_switch(:foo, :integer, "") |> parse(["--foo", "12"])
{:ok, [foo: 12]}
iex> import Commander
iex> import Commando
iex> create("app") |> with_switch(:foo, :integer, "") |> parse(["--foo", "bar"])
{:error, "Unknown options: --foo"}
"""
@spec parse(State.t, [String.t]) :: {:ok, parse_result} | :help | {:error, String.t}
def parse(commander, args) do
opts = [strict: commander.switches, aliases: commander.aliases]
def parse(commando, args) do
opts = [strict: commando.switches, aliases: commando.aliases]
case args |> OptionParser.parse(opts)
|> missing_switches(commander)
|> missing_switches(commando)
|> check_help_flag() do
:help ->
{:help, help_message(commander)}
{:help, help_message(commando)}
{result, [], []} ->
{:ok, result |> result_add_defaults(commander)}
{:ok, result |> result_add_defaults(commando)}
{_, [], missing} ->
{:error, Help.build_missing_options(missing)}
{_, invalid, _} ->
Expand All @@ -169,7 +169,7 @@ defmodule Commander do
end

@doc ~S"""
Returns a help message for the Commander instance, to be displayed with e.g.
Returns a help message for the Commando instance, to be displayed with e.g.
`IO.puts`.
Below is an example help message:
Expand All @@ -186,15 +186,15 @@ defmodule Commander do
## Examples
iex> Commander.create("demo", "Short demo app", "mix run") |>
...> Commander.with_help() |>
...> Commander.with_switch(:path, :string, "Some path", required: true, alias: :p, default: "path") |>
...> Commander.help_message()
iex> Commando.create("demo", "Short demo app", "mix run") |>
...> Commando.with_help() |>
...> Commando.with_switch(:path, :string, "Some path", required: true, alias: :p, default: "path") |>
...> Commando.help_message()
"demo - Short demo app\n\nArguments:\n --path, -p : (Required) Some path (Default: \"path\")\n --help, -h : Print help message\n\nExample: mix run"
"""
@spec help_message(State.t) :: String.t
def help_message(commander) do
Help.build_help(commander)
def help_message(commando) do
Help.build_help(commando)
end

defp missing_switches({result, _args, invalid}, state) do
Expand All @@ -204,42 +204,42 @@ defmodule Commander do
{result, invalid, missing}
end

defp check_help_flag({result, _args, _invalid}) do
defp check_help_flag({result, args, invalid}) do
if Keyword.get(result, :help) == true do
:help
else
{result, _args, _invalid}
{result, args, invalid}
end
end

@spec result_add_defaults(parse_result, State.t) :: parse_result
defp result_add_defaults(result, commander) do
defaults = commander.defaults
defp result_add_defaults(result, commando) do
defaults = commando.defaults
defaults |> Enum.reduce(result, fn ({switch, default}, result) ->
result |> Keyword.put_new(switch, default)
end)
end

@spec add_configurations(State.t, atom(), conf_list) :: State.t
defp add_configurations(commander, switch, [{:default, default} | tail]) do
commander
defp add_configurations(commando, switch, [{:default, default} | tail]) do
commando
|> State.add_default(switch, default)
|> add_configurations(switch, tail)
end
defp add_configurations(commander, switch, [{:alias, al} | tail]) do
commander
defp add_configurations(commando, switch, [{:alias, al} | tail]) do
commando
|> State.add_alias(switch, al)
|> add_configurations(switch, tail)
end
defp add_configurations(commander, switch, [{:required, true} | tail]) do
commander
defp add_configurations(commando, switch, [{:required, true} | tail]) do
commando
|> State.add_required(switch)
|> add_configurations(switch, tail)
end
defp add_configurations(commander, switch, [{:required, _} | tail]) do
commander |> add_configurations(switch, tail)
defp add_configurations(commando, switch, [{:required, _} | tail]) do
commando |> add_configurations(switch, tail)
end
defp add_configurations(commander, _switch, []) do
commander
defp add_configurations(commando, _switch, []) do
commando
end
end
4 changes: 2 additions & 2 deletions lib/commander/help.ex → lib/commando/help.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Commander.Help do
alias Commander.State
defmodule Commando.Help do
alias Commando.State

@moduledoc """
Help message builders.
Expand Down
10 changes: 5 additions & 5 deletions lib/commander/state.ex → lib/commando/state.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule Commander.State do
alias Commander.State
defmodule Commando.State do
alias Commando.State

@moduledoc """
Internal Commander state.
Internal Commando state.
"""


@typep switch :: {atom(), Commander.switch_type}
@typep switch :: {atom(), Commando.switch_type}
@typep switch_alias :: {atom(), atom()}

@type t :: %__MODULE__{app_name: String.t,
Expand All @@ -27,7 +27,7 @@ defmodule Commander.State do
aliases: [],
required: []

@spec add_switch(t, atom(), Commander.switch_type) :: t
@spec add_switch(t, atom(), Commando.switch_type) :: t
def add_switch(state, switch, type) do
%State{state | switches: state.switches |> Keyword.put(switch, type)}
end
Expand Down
8 changes: 4 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule Commander.Mixfile do
defmodule Commando.Mixfile do
use Mix.Project

def project do
[app: :commander,
[app: :commando,
version: "0.1.0",
elixir: "~> 1.4",
description: description(),
Expand Down Expand Up @@ -33,10 +33,10 @@ defmodule Commander.Mixfile do

defp package do
[
name: :commander,
name: :commando,
maintainers: ["Tobias Ara Svensson"],
licenses: ["MIT"],
links: %{"Github" => "https://github.com/svetob/commander"},
links: %{"Github" => "https://github.com/svetob/commando"},
]
end
end
5 changes: 0 additions & 5 deletions test/commander_test.exs

This file was deleted.

5 changes: 5 additions & 0 deletions test/commando_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule CommandoTest do
use ExUnit.Case

doctest Commando
end

0 comments on commit 7b809ea

Please sign in to comment.