# `Juggernaut.CLI.Options`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/cli/options.ex#L1)

Argument parsing shared by every `mix juggernaut*` task (design notes §3).

A task declares its flags once as a spec:

    [
      options: [
        lane: [type: {:enum, [:inner, :gate, :release]}, default: :gate, doc: "Preset."],
        files: [type: :string, keep: true, value: "PATH", doc: "Files to mutate."],
        format: [type: {:enum, [:human, :agent]}, default: :human, env: "JUGGERNAUT_FORMAT"]
      ],
      aliases: [f: :files],
      positional: {0, :infinity},
      exclusive: [[:files, :since, :full]]
    ]

`parse/3` runs `OptionParser` in strict mode with switches derived from the spec, then
validates and fills defaults with NimbleOptions. Every failure is a
`Juggernaut.Error` of type `:usage` (exit 2). `--help`/`-h` is always accepted.

Option types: `:boolean`, `:string`, `:pos_integer`, `:non_neg_integer`,
`{:enum, atoms}`, `:duration` (`250ms`, `90s`, `5m`, `1h` or `0`; cast to milliseconds)
and `:run_ref` (`latest`, `latest~N` or a run id; cast to `{:latest, n}` or `{:id, id}`).
A boolean whose default is `true` is documented as `--no-<name>`.

# `parsed`

```elixir
@type parsed() ::
  {:ok, keyword(), [String.t()]} | :help | {:error, Juggernaut.Error.t()}
```

# `spec`

```elixir
@type spec() :: keyword()
```

# `cast_duration`

```elixir
@spec cast_duration(String.t() | non_neg_integer()) ::
  {:ok, non_neg_integer()} | {:error, String.t()}
```

Casts `250ms`, `90s`, `5m`, `1h` or `0` to milliseconds.

# `cast_enum`

```elixir
@spec cast_enum(String.t() | atom(), [atom()]) :: {:ok, atom()} | {:error, String.t()}
```

Casts an enum value given as a string (or already an atom) to one of `choices`.

# `cast_run_ref`

```elixir
@spec cast_run_ref(String.t() | tuple()) ::
  {:ok, {:latest, non_neg_integer()} | {:id, String.t()}} | {:error, String.t()}
```

Casts `latest`, `latest~N` or a run id to `{:latest, n}` or `{:id, id}`.

# `docs`

```elixir
@spec docs(spec()) :: String.t()
```

Markdown lines documenting the flags of `spec`, for a task's `@moduledoc`.

# `parse`

```elixir
@spec parse([String.t()], spec(), %{required(String.t()) =&gt; String.t()}) :: parsed()
```

Parses `argv` against `spec`; `env` supplies environment defaults.

# `requested_format`

```elixir
@spec requested_format([String.t()], %{required(String.t()) =&gt; String.t()}, spec()) ::
  atom()
```

The output format asked for in raw `argv` or by `JUGGERNAUT_FORMAT`, read without
validating anything else, so an error in parsing can still be reported in that format.
Only the formats the task's `spec` offers count (a task without `--format` offers only
`:human`), so a parse error and an error while running are reported alike. Anything
else is `:human`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
