Skip to main content

Command

Trait Command 

Source
pub trait Command: Send + Sync {
    // Required method
    fn command() -> Command
       where Self: Sized;

    // Provided method
    fn main(
        app: Arc<App>,
        matches: ArgMatches,
    ) -> impl Future<Output = ExitCode> + Send { ... }
}
Expand description

Trait for defining CLI commands that can access the application’s dependency container.

Commands are subcommands in the CLI that can perform operations using services and components from the application. Each command defines its CLI interface and main execution logic.

§Examples

Simple command:

use diode_base::Command;
use diode::App;
use clap::{ArgMatches, Command as ClapCommand};
use std::process::ExitCode;
use std::sync::Arc;

struct StatusCommand;

impl Command for StatusCommand {
    fn command() -> ClapCommand {
        ClapCommand::new("status")
            .about("Shows application status")
    }

    async fn main(app: Arc<App>, _matches: ArgMatches) -> ExitCode {
        // Access services from the app container
        println!("Application is running");
        ExitCode::SUCCESS
    }
}

Command with arguments:

use diode_base::Command;
use diode::App;
use clap::{Arg, ArgMatches, Command as ClapCommand};
use std::process::ExitCode;
use std::sync::Arc;

struct GreetCommand;

impl Command for GreetCommand {
    fn command() -> ClapCommand {
        ClapCommand::new("greet")
            .about("Greets a user")
            .arg(Arg::new("name")
                .help("Name to greet")
                .required(true))
    }

    async fn main(_app: Arc<App>, matches: ArgMatches) -> ExitCode {
        let name = matches.get_one::<String>("name").unwrap();
        println!("Hello, {}!", name);
        ExitCode::SUCCESS
    }
}

Required Methods§

Source

fn command() -> Command
where Self: Sized,

Defines the CLI command structure for this command.

This method should return a clap::Command that defines the command name, description, arguments, and other CLI options.

§Returns

A clap::Command instance describing this command’s CLI interface.

Provided Methods§

Source

fn main( app: Arc<App>, matches: ArgMatches, ) -> impl Future<Output = ExitCode> + Send

Executes the command with the given application and parsed arguments.

This is the main entry point for command execution. The method receives the application container and the parsed command-line arguments.

§Arguments
  • app - Shared reference to the application container
  • matches - Parsed command-line arguments for this command
§Returns

Returns an ExitCode indicating the command’s execution result.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§