diff --git a/README.md b/README.md index f469a69..dcf41fe 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``` @@ -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 ``` diff --git a/config/config.exs b/config/config.exs index cfb0eb1..640f3d1 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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: # diff --git a/lib/commander.ex b/lib/commando.ex similarity index 66% rename from lib/commander.ex rename to lib/commando.ex index abe01a4..ca57b25 100644 --- a/lib/commander.ex +++ b/lib/commando.ex @@ -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. @@ -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 @@ -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 @@ -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) @@ -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 @@ -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, _} -> @@ -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: @@ -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 @@ -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 diff --git a/lib/commander/help.ex b/lib/commando/help.ex similarity index 96% rename from lib/commander/help.ex rename to lib/commando/help.ex index 9aa4c84..fcaadc1 100644 --- a/lib/commander/help.ex +++ b/lib/commando/help.ex @@ -1,5 +1,5 @@ -defmodule Commander.Help do - alias Commander.State +defmodule Commando.Help do + alias Commando.State @moduledoc """ Help message builders. diff --git a/lib/commander/state.ex b/lib/commando/state.ex similarity index 87% rename from lib/commander/state.ex rename to lib/commando/state.ex index caf5fac..f15636d 100644 --- a/lib/commander/state.ex +++ b/lib/commando/state.ex @@ -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, @@ -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 diff --git a/mix.exs b/mix.exs index 136c3cd..7afa551 100644 --- a/mix.exs +++ b/mix.exs @@ -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(), @@ -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 diff --git a/test/commander_test.exs b/test/commander_test.exs deleted file mode 100644 index 1d426f6..0000000 --- a/test/commander_test.exs +++ /dev/null @@ -1,5 +0,0 @@ -defmodule CommanderTest do - use ExUnit.Case - - doctest Commander -end diff --git a/test/commando_test.exs b/test/commando_test.exs new file mode 100644 index 0000000..ccbe3fa --- /dev/null +++ b/test/commando_test.exs @@ -0,0 +1,5 @@ +defmodule CommandoTest do + use ExUnit.Case + + doctest Commando +end