alux-cli-direct 0.2.1

Direct command-line interpretation of ALUX CLI programs: parses arguments, applies operations, renders help
Documentation
//! Runs a single-binary program configured from options and the environment, as a node is.

use alux_cli::{CliCommandsAlg, CliProgramBuilder, cli};
use alux_cli_direct::{CliDirect, Exit};
use alux_ext::ext;
use std::{net::IpAddr, sync::Arc};

trait Node {
    fn name(&self) -> String;
}

#[ext(name = NodeExt, defunc)]
impl<This> This
where
    This: Node,
{
    /// Runs the node.
    fn run_node(&self, dev: bool, rpc_addr: Option<IpAddr>, peers: Vec<String>) -> String {
        format!("{} dev={dev} rpc={rpc_addr:?} peers={peers:?}", self.name())
    }

    /// Checks the configuration.
    fn check(&self, file: String) -> String {
        format!("checked {file}")
    }

    /// Reports whether it is quiet.
    fn quiet(&self, quiet: bool) -> String {
        format!("quiet={quiet}")
    }
}

#[ext(name = NodeCliExt, defunc(via = cli))]
impl<This> This
where
    This: CliCommandsAlg,
{
    fn node_cli<Machine>(&self) {
        self.commands()
            .cmd_root(
                self.op(Machine::run_node)
                    .arg("dev", self.named("Development mode").switch().env("NEXTEST"))
                    .arg("rpc_addr", self.named("RPC address").optional())
                    .arg("peers", self.named("Peers to dial").many().env("CARGO_PKG_VERSION").delimiter('.')),
            )
            .cmd("check", self.op(Machine::check).arg("file", self.positional("Configuration file")))
            .cmd(
                "quiet",
                self.op(Machine::quiet).arg("quiet", self.named("Quiet").switch().env("CARGO_PKG_VERSION_PRE")),
            )
    }

    /// Declares the same command line in A-normal form: each part bound once, then named.
    fn node_anf_cli<Machine>(&self) {
        let dev = self.named("Development mode").switch().env("NEXTEST");
        let rpc_addr = self.named("RPC address").optional();
        let peers = self.named("Peers to dial").many().env("CARGO_PKG_VERSION").delimiter('.');
        let run = self.op(Machine::run_node).arg("dev", dev).arg("rpc_addr", rpc_addr).arg("peers", peers);
        let check = self.op(Machine::check).arg("file", self.positional("Configuration file"));
        let quiet = self.op(Machine::quiet).arg("quiet", self.named("Quiet").switch().env("CARGO_PKG_VERSION_PRE"));
        self.commands().cmd_root(run).cmd("check", check).cmd("quiet", quiet)
    }
}

struct Local;

impl Node for Local {
    fn name(&self) -> String {
        "local".to_owned()
    }
}

fn node(line: &str) -> Exit {
    CliDirect::new(Arc::new(Local))
        .version("1.2.3")
        .run_blocking(CliProgramBuilder.node_cli::<Local>(), line.split_whitespace())
}

#[test]
fn runs_the_programs_own_command_with_no_command_word() {
    // nextest sets `NEXTEST=1` and Cargo sets `CARGO_PKG_VERSION` for every test it runs, so the
    // switch is on and the peers are this package's version split at its dots.
    let peers = env!("CARGO_PKG_VERSION").split('.').collect::<Vec<_>>();
    assert_eq!(node("node").stdout, format!("local dev=true rpc=None peers={peers:?}\n"));
    // The command line wins over the environment, and each of its words is split too.
    assert_eq!(
        node("node --dev --rpc-addr 127.0.0.1 --peers a.b --peers c").stdout,
        "local dev=true rpc=Some(127.0.0.1) peers=[\"a\", \"b\", \"c\"]\n"
    );
    // A named command is selected before the program's own.
    assert_eq!(node("node check node.toml").stdout, "checked node.toml\n");
}

#[test]
fn answers_version_and_helps_the_programs_own_command_with_the_others() {
    assert_eq!(node("node --version"), Exit { status: 0, stdout: "node 1.2.3\n".into(), stderr: String::new() });
    let help = node("node --help").stdout;
    assert!(help.starts_with("Usage: node [options]\n       node <command>\n"), "{help}");
    assert!(help.contains("check") && help.contains("--version"), "{help}");
    // The switch lists the variable it falls back to, and the list its delimiter.
    let line = |spelling: &str| help.lines().find(|line| line.contains(spelling)).unwrap_or_default().to_owned();
    assert!(line("--dev").ends_with("Development mode [env: NEXTEST]"), "{help}");
    // The delimiter here is `.`, so the list reads `<peers>.` followed by `...`.
    assert!(line("--peers").contains("--peers <peers>...."), "{help}");
    // A value is named as the command line spells it.
    assert!(line("--rpc-addr").contains("--rpc-addr <rpc-addr>  RPC address"), "{help}");
}

#[test]
fn reads_a_declaration_in_a_normal_form_as_its_nested_form() {
    let anf = |line: &str| {
        CliDirect::new(Arc::new(Local))
            .version("1.2.3")
            .run_blocking(CliProgramBuilder.node_anf_cli::<Local>(), line.split_whitespace())
    };
    for line in
        ["node", "node --dev --rpc-addr 127.0.0.1 --peers a.b", "node check node.toml", "node --help", "node --nope"]
    {
        assert_eq!(anf(line), node(line), "{line}");
    }
}

#[test]
fn turns_a_switch_off_as_its_spelling_or_an_empty_variable_says() {
    let peers = env!("CARGO_PKG_VERSION").split('.').collect::<Vec<_>>();
    let dev = |line: &str| node(line).stdout.replace(&format!(" rpc=None peers={peers:?}\n"), "");
    // A long spelling states a truth, in any case, over `NEXTEST=1`.
    assert_eq!(dev("node --dev=false"), "local dev=false");
    assert_eq!(dev("node --dev=OFF"), "local dev=false");
    assert_eq!(dev("node --dev=0"), "local dev=false");
    assert_eq!(dev("node --dev=On"), "local dev=true");
    assert_eq!(dev("node --dev"), "local dev=true");
    assert_eq!(node("node --dev=maybe").status, 2);
    // Cargo sets `CARGO_PKG_VERSION_PRE` to nothing for a release, which states nothing.
    assert_eq!(node("node quiet").stdout, "quiet=false\n");
    assert_eq!(node("node quiet --quiet").stdout, "quiet=true\n");
}