argx 0.1.1

Expressive command-line parsing and configuration for Rust.
Documentation
//! Shared support for executable integration tests.

use std::{path::PathBuf, sync::OnceLock};

use snapbox::cmd::Command;

/// Compiled path for the basic public example.
static BASIC_EXAMPLE: OnceLock<PathBuf> = OnceLock::new();
/// Compiled path for the arguments public example.
static ARGUMENTS_EXAMPLE: OnceLock<PathBuf> = OnceLock::new();
/// Compiled path for the commands public example.
static COMMANDS_EXAMPLE: OnceLock<PathBuf> = OnceLock::new();

/// Returns a deterministic command targeting one real public example.
pub(crate) fn example_command(name: &str) -> Command {
    let binary = match name {
        "basic" => BASIC_EXAMPLE.get_or_init(|| compile_example("basic")),
        "arguments" => ARGUMENTS_EXAMPLE.get_or_init(|| compile_example("arguments")),
        "commands" => COMMANDS_EXAMPLE.get_or_init(|| compile_example("commands")),
        other => panic!("unsupported CLI-test example `{other}`"),
    };
    Command::new(binary).env("NO_COLOR", "1")
}

/// Compiles one public example for executable integration testing.
fn compile_example(name: &str) -> PathBuf {
    snapbox::cmd::compile_example(name, ["--all-features"])
        .unwrap_or_else(|error| panic!("failed to compile {name} example: {error}"))
}