Juggernaut.CLI.Options (Juggernaut v0.4.0)

Copy Markdown View Source

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>.

Summary

Functions

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

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

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

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

Parses argv against spec; env supplies environment defaults.

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.

Types

parsed()

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

spec()

@type spec() :: keyword()

Functions

cast_duration(ms)

@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(value, choices)

@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(ref)

@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(spec)

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

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

parse(argv, spec, env \\ System.get_env())

@spec parse([String.t()], spec(), %{required(String.t()) => String.t()}) :: parsed()

Parses argv against spec; env supplies environment defaults.

requested_format(argv, env, spec)

@spec requested_format([String.t()], %{required(String.t()) => 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.