Arguments v0.1.0 Arguments View Source

Arguments provides a module with argument parsing through YourArguments.parse(incoming)

use Arguments

There are two styles of arguments allowed

  • command - $ cmd new project
  • flag - $ cmd --dir /etc

These two styles can be mixed, but the flags will always take priority

Full Example:

module MyArguments do
  use Arguments

  command "new", do: [
    arguments: [:name, :dir]
  ]

  flag "name", do: [
    type: :string,
    alias: :n,
    defaults: fn (n) -> [
      dir: "./#{n}"
    ] end
  ]

  flag "more", do: %{
    type: :boolean,
    alias: :m
  }

  flag "dir", do: [
    type: :string
  ]
end
iex> MyArguments.parse(["--name", "myname"])
%{name: "myname", dir: "./myname"}

iex> MyArguments.parse(["new", "myname", "dirhere"])
%{new: true, name: "myname", dir: "dirhere"}

iex> MyArguments.parse(["--more"])
%{more: true}

iex> MyArguments.parse(["-m"])
%{more: true}

Link to this section Summary

Functions

Defines a command style argument

Defines a flag style argument

Link to this section Functions

Link to this macro command(name, list) View Source (macro)

Defines a command style argument

A command is an ordered set of flags with a boolean flag for itself

Required Options:

  • arguments: (list of atoms) [:arg0, arg1] e.g. [:name, :dir]

    • Example

      • command "new", do [arguments: [:name, :dir]]
      • $ cmd new MyName YourDir
      • %{new: true, name: "MyName", dir: "YourDir"}

Optional Uncommon Options:

  • name: (atom) Double dash name, e.g. :dir -> --dir (defaults to command [name])
Link to this macro flag(name, list) View Source (macro)

Defines a flag style argument

Required Options:

  • type: (atom) Argument type from OptionParser.parse/1

    • :string parses the value as a string
    • :boolean sets the value to true when given
    • :count counts the number of times the switch is given
    • :integer parses the value as an integer
    • :float parses the value as a float

Optional Common Options:

  • alias: (atom) Single dash name, e.g. :d -> -d
  • defaults: (fn(value) | list) If the flag is set, the defaults will be applied

    • The end result must be a keyword list. The function form may NOT use anything from outside, but will be passed in the value of the flag.
    • Example:

      • defaults: fn(name) -> [dir: "./#{name}"] end
      • $ cmd --name myname
      • %{name: "myname", dir: "./myname"}

Optional Uncommon Options:

  • name: (atom) Double dash name, e.g. :dir -> --dir (defaults to flag [name])