Cheer (Cheer v0.1.5)

Copy Markdown View Source

A clap-inspired CLI argument parsing framework for Elixir.

Provides declarative command definitions with arbitrarily nested subcommands, typed options, automatic help generation, and shell completion.

Usage

defmodule MyApp.CLI.Greet do
  use Cheer.Command

  command "greet" do
    about "Greet someone"

    argument :name, type: :string, required: true, help: "Name to greet"
    option :loud, type: :boolean, short: :l, help: "Shout the greeting"
  end

  @impl Cheer.Command
  def run(%{name: name} = args, _opts) do
    greeting = "Hello, #{name}!"
    if args[:loud], do: String.upcase(greeting), else: greeting
  end
end

Architecture

Commands are modules that use Cheer.Command. Each command declares its name, about text, arguments, options, and subcommands via macros. At compile time, Cheer builds a command tree that handles:

  • Argv routing through nested subcommands
  • Option parsing via OptionParser
  • Type validation and coercion
  • Help text generation
  • Shell completion script generation (bash, zsh, fish)

Summary

Functions

Parse argv and dispatch to the appropriate command handler.

Returns the command tree as a nested data structure.

Functions

run(root_command, argv, opts \\ [])

@spec run(module(), [String.t()], keyword()) :: term()

Parse argv and dispatch to the appropriate command handler.

Options:

  • :prog - program name for usage lines (default: derived from root command name)

tree(command)

@spec tree(module()) :: map()

Returns the command tree as a nested data structure.

Useful for documentation generation, introspection, and testing.