View Source Prompt.Router behaviour (prompt v0.10.0)

Router for Prompt

Simplifies defining commands, sub-commands and arguments.

Choose the module responsible for taking the command line arguments and use Prompt.Router, otp_app: :your_app at the top.

Then simply define your commands and arguments.

Exposes a main/1 function that is called with the command line args

arguments

Arguments

See arg/3

example

Example

defmodule My.CLI do
  use Prompt.Router, otp_app: :my_app

  command :checkout, My.CheckoutCommand do
    arg :help, :boolean
    arg :branch, :string, short: :b, default: "main"
  end

  command "", My.DefaultCommand do
    arg :info, :boolean
  end
end

defmodule My.CheckoutCommand do
  use Prompt.Command

  @impl true
  def process(arguments) do
    # arguments will be a map of the defined arguments and their values
    # from the command line input
    # If someone used the command and passed `--branch  feature/test`, then
    # `argmuments would look like `%{help: false, branch: "feature/test"}`
    display("checking out " <> arguments.branch)
  end
end

defmodule My.DefaultCommand do
  use Prompt.Command

  @impl true
  def init(arguments) do
    # you can implement the `c:init/1` callback to transform
    # the arguments before `c:process/1` is called if you want
    arguments
  end
  
  @impl true
  def process(arguments) do
    # arguments will have a key of `:leftover` for anything
    # passed to the command that doesn't have a `arg` defined.
    # IF someone called this with `--info --test something`, then then
    # arguments will look like `%{info: true, leftover: ["--test", "something"]}`
    display("showing info")
  end
end

Link to this section Summary

Callbacks

This function is called after the main function is done.

Prints help to the screen when there is an error, or --help is passed as an argument.

Prints help to the screen when there is an error with a string indicating the error

The function responsible for filtering and calling the correct command module based on command line input

Prints the version from the projects mix.exs file

Functions

Defines the arguments of a command.

Name of the subcommand that is expectedaany()

Link to this section Callbacks

@callback handle_exit_value(any()) :: no_return()

This function is called after the main function is done.

It does it's best to handle any value returned from a command and turn it into an integer, 0 being a successful command and any non-zero being an error.

Overrideable

@callback help() :: non_neg_integer()

Prints help to the screen when there is an error, or --help is passed as an argument.

Overridable

@callback help(String.t()) :: non_neg_integer()

Prints help to the screen when there is an error with a string indicating the error

Overridable

@callback main([binary()]) :: no_return()

The function responsible for filtering and calling the correct command module based on command line input

@callback version() :: non_neg_integer()

Prints the version from the projects mix.exs file

Overridable

Link to this section Functions

Link to this macro

arg(arg_name, arg_type, opts \\ [])

View Source (macro)

Defines the arguments of a command.

argument-name

Argument Name

This indicates what the user will type as the option to the sub-command. For example,

arg :print, :boolean

would allow the user to type $ your_command --print

options

Options

Available options are:

  • default - a default value if the user doesn't use this option
  • short - an optional short argument option i.e short: :h would all the user to type -h
Link to this macro

command(name, module)

View Source (macro)
Link to this macro

command(name, module, list)

View Source (macro)

Name of the subcommand that is expectedaany()

Takes an atom or string as the command name and a Prompt.Command module.