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
impl CommandRunner
Sourcepub fn run<E, F, T>(matches: ArgMatches, exec: E) -> Exitwhere
E: FnOnce(ArgMatches, Context) -> F,
F: Future<Output = CommandResult<T>> + Send + 'static,
T: Into<Exit>,
pub fn run<E, F, T>(matches: ArgMatches, exec: E) -> Exitwhere
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:
- Detects whether a user is present who can answer the application
- Creates a
Cancellationtoken for cooperative shutdown - Extracts output flags from the pre-parsed argument matches
- Creates an event channel and builds the
Context - Configures a
TerminalPresenterwith the parsed output flags - 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 parsedArgMatchesfor 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 anExitto choose the exit code and the text.