pub struct CommandRegistry { /* private fields */ }Expand description
Registry of named commands.
Commands registered here are used by handle_pipeline and
handle_parallel to dispatch each stage of a pipeline.
§Examples
ⓘ
use wasi_shell::{CommandRegistry, handle_pipeline, IoContext};
use std::io::{self, Write};
let mut reg = CommandRegistry::with_builtins();
// Add a custom command
reg.register("greet", |args, ctx| {
let name = args.get(1).map(|s| s.as_str()).unwrap_or("world");
writeln!(ctx.stdout, "Hello, {}!", name).map_err(|e| e.to_string())
});
handle_pipeline("greet Rust", Box::new(io::empty()), Box::new(io::stdout()), ®, wasibox_core::CancellationToken::new()).unwrap();Implementations§
Source§impl CommandRegistry
impl CommandRegistry
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty registry with a wasibox_core fallback.
Any command name not explicitly registered will be forwarded to
wasibox_core::execute_with_context.
Sourcepub fn with_builtins() -> Self
pub fn with_builtins() -> Self
Create a registry pre-loaded with shell built-in commands
(cd, help, exit, sl) and a wasibox_core fallback for all
core utilities (echo, cat, grep, seq, head, …).
Sourcepub fn command_names(&self) -> Vec<&str>
pub fn command_names(&self) -> Vec<&str>
Returns the names of all explicitly registered commands (excluding the fallback).
Sourcepub fn register<F>(&mut self, name: impl Into<String>, handler: F)
pub fn register<F>(&mut self, name: impl Into<String>, handler: F)
Register (or replace) a command.
Sourcepub fn set_fallback<F>(&mut self, handler: F)
pub fn set_fallback<F>(&mut self, handler: F)
Set a fallback handler invoked when no explicit command matches.
Sourcepub fn remove_fallback(&mut self)
pub fn remove_fallback(&mut self)
Remove the fallback handler. Unknown commands will return an error.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for CommandRegistry
impl !UnwindSafe for CommandRegistry
impl Freeze for CommandRegistry
impl Send for CommandRegistry
impl Sync for CommandRegistry
impl Unpin for CommandRegistry
impl UnsafeUnpin for CommandRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more