Skip to main content

AddCommandExt

Trait AddCommandExt 

Source
pub trait AddCommandExt {
    // Required methods
    fn add_command<T>(&mut self) -> &mut Self
       where T: Command + 'static;
    fn has_command<T>(&self) -> bool
       where T: Command + 'static;
}
Expand description

Extension trait for AppBuilder to add command registration methods.

This trait provides convenient methods for registering commands with the application builder. Commands registered this way will be available in the CLI interface when the application is run.

Required Methods§

Source

fn add_command<T>(&mut self) -> &mut Self
where T: Command + 'static,

Registers a command with the application builder.

The command will be available as a subcommand in the CLI interface. If this is the first command being added, a CommandRegistry will be automatically created and added to the application.

§Type Parameters
  • T - The command type to register. Must implement Command + 'static.
§Returns

Returns &mut Self for method chaining.

§Examples
use diode::{App, AppBuilder};
use diode_base::{Command, AddCommandExt};
use clap::Command as ClapCommand;
use std::process::ExitCode;
use std::sync::Arc;

struct MyCommand;
impl Command for MyCommand {
    fn command() -> ClapCommand { ClapCommand::new("my-cmd") }
}

let app = App::builder()
    .add_command::<MyCommand>()
    .build()
    .await?;
Source

fn has_command<T>(&self) -> bool
where T: Command + 'static,

Checks if a command type has been registered.

§Type Parameters
  • T - The command type to check for.
§Returns

Returns true if the command is registered, false otherwise.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl AddCommandExt for AppBuilder

Source§

fn add_command<T>(&mut self) -> &mut Self
where T: Command + 'static,

Source§

fn has_command<T>(&self) -> bool
where T: Command + 'static,

Implementors§