Skip to main content

ReplInterface

Struct ReplInterface 

Source
pub struct ReplInterface { /* private fields */ }
Expand description

REPL (Read-Eval-Print Loop) interface

Provides an interactive command-line interface with:

  • Line editing and history
  • Per-application persistent command history
  • Tab completion (commands, aliases, option flags)
  • Graceful error handling
  • Special commands (exit, quit, –help)

§Architecture

User input → rustyline (DcliHelper) → ReplParser → CommandExecutor → Handler
                   ↓                                      ↓
            Tab completion                         ExecutionContext
         (commands + flags)

§Special Commands

The REPL recognizes these built-in commands:

  • exit, quit — Exit the REPL
  • --help, -h — Show application-level help (if a formatter is attached)
  • <cmd> --help, --help <cmd> — Show per-command help

§History

Command history is stored per application under the XDG data directory:

  • Linux/macOS: ~/.local/share/<app_name>/history
  • Windows: %LOCALAPPDATA%\<app_name>\history

Lines containing a secure: true argument are never written to history. Lines that fail to parse are discarded silently.

Implementations§

Source§

impl ReplInterface

Source

pub fn new( registry: CommandRegistry, context: Box<dyn ExecutionContext>, prompt: String, config: Option<CommandsConfig>, help_formatter: Option<Box<dyn HelpFormatter>>, ) -> Result<Self>

Create a new REPL interface.

All configuration is supplied at construction time so that the tab-completion engine and the help formatter share the same data without duplication.

§Arguments
  • registry — Command registry with all registered commands.
  • context — Execution context passed to handlers.
  • prompt — Prompt prefix (e.g., "myapp"). The displayed prompt is prompt followed by config.metadata.prompt_suffix (e.g., "myapp > ") when config is supplied, or by [crate::config::schema::default_prompt_suffix] ("myapp > ") otherwise — the suffix is never hardcoded independently of the config.
  • config — Application configuration for completion, help, and the prompt suffix. Pass None to disable all three.
  • help_formatter — Help formatter implementation. Pass None to use [DefaultHelpFormatter] lazily, or supply a custom implementation.
§Errors

Returns an error if rustyline initialisation fails (rare).

§Example
use dynamic_cli::interface::ReplInterface;
use dynamic_cli::prelude::*;

let registry = CommandRegistry::new();
let context = Box::new(MyContext::default());

// Without completion or help:
let repl = ReplInterface::new(registry, context, "myapp".to_string(), None, None)?;
Source

pub fn with_prompt_multiline(self, prompt: String) -> Self

Override the base segment of the continuation prompt shown while a \-continued command is being accumulated (DD-027, #67).

prompt here plays the same role as the app-name segment passed to ReplInterface::new for the main prompt: only the base is supplied, prompt_suffix (from the config, or the default) is always appended after it by effective_prompt_multiline. When this setter is never called, the base defaults to "...".

Purely additive — consuming self and returning Self like every other fluent setter in this crate; no existing ReplInterface::new() call site changes.

§Example
use dynamic_cli::interface::ReplInterface;
use dynamic_cli::prelude::*;

let registry = CommandRegistry::new();
let context = Box::new(MyContext::default());

let repl = ReplInterface::new(registry, context, "rpn".to_string(), None, None)?
    .with_prompt_multiline("_ _ _".to_string());
Source

pub fn run(self) -> Result<()>

Run the REPL loop.

Enters an interactive loop that:

  1. Displays the prompt (or the continuation prompt while accumulating a \-continued command, DD-027/#69)
  2. Reads user input (with tab completion)
  3. Parses and executes the command
  4. Displays results or errors
  5. Repeats until the user exits
§Multi-line option accumulation (DD-027, #69)

A line ending in a trailing \ is not dispatched: the marker is stripped and the fragment is buffered, the prompt switches to effective_prompt_multiline, and another line is read. The first line that does not end in \ completes the buffer — every fragment plus this final one are joined with a single space and dispatched through execute_line exactly once, with the normal prompt restored. Only this fully reconstructed line (no \ markers) can ever reach REPL history — execute_line() never sees a raw partial fragment.

A Ctrl+C while accumulating discards the buffer and returns to the normal prompt, mirroring the familiar shell convention of abandoning a continued line on interrupt.

This mechanism is entirely local to this loop: Self::execute_line, ReplParser, and CliParser are untouched, and non-interactive paths (run_script(), :load) never go through it — see DD-027’s abort-by-construction rule for contexts without a live operator.

§Returns
  • Ok(()) when the user exits normally (via exit or quit)
  • Err(_) on critical errors (I/O failures, etc.)
§Example
use dynamic_cli::interface::ReplInterface;
use dynamic_cli::prelude::*;

let registry = CommandRegistry::new();
let context = Box::new(MyContext::default());

let repl = ReplInterface::new(registry, context, "myapp".to_string(), None, None)?;
repl.run()?;

Trait Implementations§

Source§

impl Drop for ReplInterface

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.