Skip to main content

NativeCommand

Trait NativeCommand 

Source
pub trait NativeCommand: Send + Sync {
    // Required methods
    fn command(&self) -> Command;
    fn execute(
        &self,
        args: &[String],
        context: &NativeCommandContext<'_>,
    ) -> Result<NativeCommandOutcome>;

    // Provided methods
    fn auth(&self) -> Option<DescribeCommandAuthV1> { ... }
    fn describe(&self) -> DescribeCommandV1 { ... }
}
Expand description

Trait implemented by in-process commands registered alongside plugins.

Required Methods§

Source

fn command(&self) -> Command

Returns the clap command definition for this command.

Source

fn execute( &self, args: &[String], context: &NativeCommandContext<'_>, ) -> Result<NativeCommandOutcome>

Executes the command using already-parsed argument tokens.

args contains the tokens after the registered command name. For a command registered as history, the command line osp history clear --all reaches execute as ["clear", "--all"].

The host interprets outcomes as follows:

Return Err when command execution itself fails. The host formats that failure like other command errors.

§Examples
use anyhow::Result;
use clap::Command;
use osp_cli::{NativeCommand, NativeCommandContext, NativeCommandOutcome};

struct HistoryCommand;

impl NativeCommand for HistoryCommand {
    fn command(&self) -> Command {
        Command::new("history").about("Manage local history")
    }

    fn execute(
        &self,
        args: &[String],
        _context: &NativeCommandContext<'_>,
    ) -> Result<NativeCommandOutcome> {
        match args {
            [subcommand, flag] if subcommand == "clear" && flag == "--all" => {
                Ok(NativeCommandOutcome::Exit(0))
            }
            _ => Ok(NativeCommandOutcome::Help(
                "usage: history clear --all".to_string(),
            )),
        }
    }
}

Provided Methods§

Source

fn auth(&self) -> Option<DescribeCommandAuthV1>

Returns optional auth/visibility metadata for the command.

Source

fn describe(&self) -> DescribeCommandV1

Builds the plugin-protocol style description for this command.

Implementors§