Skip to main content

cli_handle_version_or_continue

Function cli_handle_version_or_continue 

Source
pub fn cli_handle_version_or_continue(
    raw_args: &[String],
    cmd: &Command,
    name: &str,
    display_name: Option<&str>,
    version: &str,
    build: Option<&str>,
) -> Result<Option<String>, Event>
Expand description

Render version output from raw argv if --version or -V is present.

raw_args should be the full argv vector, including argv[0], as produced by std::env::args(). The helper intentionally runs before clap or another parser so explicit --output json|yaml|plain is honored instead of being bypassed by built-in version handling. cmd is the caller’s own clap::Command (typically Cli::command()) — used to inherit the declared --output default and to look up which flags take a value, so any global flag the caller defines (--stdout-file, or one added later) is recognized without the pre-parser having to hardcode its name.

Only a top-level version request is recognized: scanning stops at the first positional argument (the subcommand), so tool sub --version <value> leaves --version for the subcommand’s parser rather than printing the tool version. That boundary check is unconditional and runs before any flag is inspected — it is unaffected by cmd, which only decides how many argv slots a recognized flag consumes, so a flag’s value is never mistaken for it.

The one blessed behavior: --version always answers with a protocol-v1 kind:"result" version event (payload { "code": "version", "name", ... }, see build_cli_version). An explicit --output/--json wins; otherwise the handler inherits the command’s declared --output default, falling back to JSON. Returns a standard build_cli_error event when the request is malformed, for example --version --output xml.