Raxol.Command.Parser (Raxol v2.0.1)

View Source

Command parser with tab completion, history, and argument parsing.

Features:

  • Command tokenization with quoted strings
  • Flag and argument parsing
  • Tab completion
  • Command history (↑/↓ navigation)
  • Fuzzy history search (Ctrl+R)
  • Command aliases

Example

parser = Parser.new()
parser = Parser.register_command(parser, "echo", &echo_handler/1)
parser = Parser.register_alias(parser, "e", "echo")

{:ok, result, parser} = Parser.parse_and_execute(parser, "echo Hello World")
# => {:ok, "Hello World", parser}

Tab Completion

parser = Parser.handle_key(parser, "ec")
parser = Parser.handle_key(parser, "Tab")
# Autocompletes to "echo"

Summary

Functions

Get completion candidates for current input.

Get cursor position.

Get command history.

Get current input string.

Handle a key press for interactive command input.

Create a new command parser.

Parse and execute a command string.

Register a command handler.

Types

command_fn()

@type command_fn() :: ([String.t()] -> {:ok, any()} | {:error, String.t()})

t()

@type t() :: %Raxol.Command.Parser{
  aliases: map(),
  commands: map(),
  completion_candidates: [String.t()],
  completion_index: non_neg_integer(),
  cursor_pos: non_neg_integer(),
  history: [String.t()],
  history_index: non_neg_integer(),
  input: String.t(),
  search_mode: boolean(),
  search_query: String.t()
}

Functions

get_completions(map)

@spec get_completions(t()) :: [String.t()]

Get completion candidates for current input.

get_cursor_pos(map)

@spec get_cursor_pos(t()) :: non_neg_integer()

Get cursor position.

get_history(map)

@spec get_history(t()) :: [String.t()]

Get command history.

get_input(map)

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

Get current input string.

handle_key(parser, key)

@spec handle_key(t(), String.t()) :: t()

Handle a key press for interactive command input.

new()

@spec new() :: t()

Create a new command parser.

parse_and_execute(parser, command_str)

@spec parse_and_execute(t(), String.t()) ::
  {:ok, any(), t()} | {:error, String.t(), t()}

Parse and execute a command string.

register_alias(parser, alias_name, command_name)

@spec register_alias(t(), String.t(), String.t()) :: t()

Register a command alias.

register_command(parser, name, handler)

@spec register_command(t(), String.t(), command_fn()) :: t()

Register a command handler.