Skip to main content

CommandRunner

Struct CommandRunner 

Source
pub struct CommandRunner;
Expand description

CLI command runner

Encapsulates the lifecycle of running a CLI command: creating the event channel, constructing the Context, registering signal handlers, and presenting output through a TerminalPresenter. The main!() macro dispatches to CommandRunner::run when the resolved leaf is a ResolvedLeaf::Command.

§Examples

ⓘ
// This is what main!() expands to for commands:
ResolvedLeaf::Command { matches, exec } => {
    clawless::runner::CommandRunner::run(matches, exec)
}

Implementations§

Source§

impl CommandRunner

Source

pub fn run<E, F, T>(matches: ArgMatches, exec: E) -> Exit
where E: FnOnce(ArgMatches, Context) -> F, F: Future<Output = CommandResult<T>> + Send + 'static, T: Into<Exit>,

Runs a CLI command to completion

Sets up the command lifecycle:

  1. Detects whether a user is present who can answer the application
  2. Creates a Cancellation token for cooperative shutdown
  3. Extracts output flags from the pre-parsed argument matches
  4. Creates an event channel and builds the Context
  5. Configures a TerminalPresenter with the parsed output flags
  6. Creates a Tokio runtime, spawns the signal handler, and runs the command through the presenter

A user is present when the standard input and the standard error are both terminals. The Context carries the result as its Interactivity.

Output flags (--quiet, --verbose, --json) are augmented at the root level by main!() with .global(true), so they are available in every leaf’s ArgMatches.

The runner returns the Exit of the command, and only after the presenter has rendered every event of the command. If the command fails, or if the runner cannot build the Context or the Tokio runtime, the Exit has the exit code 1 and the text of the error. The main function returns the Exit in a ProcessExit, and the process then writes its text and exits with its code. The text therefore comes after all the output of the command.

§Arguments
  • matches — The parsed ArgMatches for this command leaf, as resolved by the subcommand tree walk.
  • exec — Executes the command with the given matches and context. The #[command] macro generates a function for this, and any other callable works. A caller that builds its command tree at run time passes a closure that owns the command it resolved. The command returns () for the exit code 0, or an Exit to choose the exit code and the text.

Trait Implementations§

Source§

impl Debug for CommandRunner

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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, 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.