xa-cli 1.0.0

A modern, safe replacement for xargs
use std::io::Error;
use std::path::PathBuf;

use clap::CommandFactory;
use clap_complete::{Shell, generate_to};
use clap_mangen::Man;

// Pull in the CLI definition so we can generate artefacts from it.
// The `delimiter_byte` method is used in the main binary but not here — suppress the lint.
#[allow(dead_code)]
#[path = "src/cli.rs"]
mod cli;

fn main() -> Result<(), Error> {
    // OUT_DIR is the only directory build scripts are allowed to write to.
    let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());

    // Shell completions
    let completions_dir = out_dir.join("completions");
    std::fs::create_dir_all(&completions_dir)?;
    let mut cmd = cli::Cli::command();
    for shell in [Shell::Bash, Shell::Zsh, Shell::Fish] {
        generate_to(shell, &mut cmd, "xa", &completions_dir)?;
    }

    // Man page
    let man_dir = out_dir.join("man");
    std::fs::create_dir_all(&man_dir)?;
    let cmd = cli::Cli::command();
    let man = Man::new(cmd);
    let mut buf = Vec::new();
    man.render(&mut buf)?;
    std::fs::write(man_dir.join("xa.1"), buf)?;

    Ok(())
}