Alaja.CLI.Definition (Alaja v1.0.0)

Copy Markdown View Source

Declarative DSL for defining CLI commands.

Usage

defmodule MyApp.CLI do
  use Alaja.CLI.Definition, otp_app: :my_app

  command "deploy", "Deploy to production" do
    flag :env, :string, default: "staging", values: ~w(staging production)
    flag :force, :boolean, default: false

    run fn opts ->
      IO.puts("Deploying to " <> opts.env)
      if opts.force, do: IO.puts("Forced mode!")
    end
  end

  subcommand "config", "Manage configuration" do
    command "get", "Read a value" do
      argument :key, :string, required: true
      run fn opts -> IO.inspect(opts.key) end
    end
  end
end

Structure

The DSL generates a command map with the following shape:

%{
  name: "deploy",
  description: "Deploy to production",
  flags: [...],
  arguments: [...],
  subcommands: %{},
  run: function
}

Summary

Functions

Defines a positional argument within a command.

Defines a CLI command with a name, description, and block.

Defines a CLI flag within a command.

Defines the handler function for a command.

Defines a CLI subcommand group.

Types

arg_type()

@type arg_type() :: :string | :integer | :float

flag_type()

@type flag_type() :: :string | :integer | :float | :boolean | :atom

Functions

argument(name, type, opts \\ [])

(macro)
@spec argument(atom(), arg_type(), Keyword.t()) :: Macro.t()

Defines a positional argument within a command.

command(name, description, list)

(macro)
@spec command(String.t(), String.t(), [{:do, Macro.t()}]) :: Macro.t()

Defines a CLI command with a name, description, and block.

flag(name, type, opts \\ [])

(macro)
@spec flag(atom(), flag_type(), Keyword.t()) :: Macro.t()

Defines a CLI flag within a command.

run(function)

(macro)
@spec run((map() -> any())) :: Macro.t()

Defines the handler function for a command.

subcommand(name, description, list)

(macro)
@spec subcommand(String.t(), String.t(), [{:do, Macro.t()}]) :: Macro.t()

Defines a CLI subcommand group.