argx 0.3.0

Expressive command-line parsing and configuration for Rust.
Documentation
//! Generates a dynamic shell-completion adapter.
//!
//! Run it with:
//!
//! ```text
//! cargo run --example completions -- bash
//! cargo run --example completions -- fish
//! cargo run --example completions -- nushell
//! cargo run --example completions -- zsh
//! ```
//!
//! The generated script is written to stdout, so it can be inspected or saved directly:
//!
//! ```text
//! cargo run --quiet --example completions -- zsh | less
//! cargo run --quiet --example completions -- zsh > /tmp/_completions
//! ```

use std::io::{self, Write};

use argx::{Parser, completion::Shell};

/// Completion-script generator for this example binary.
#[derive(Debug, Parser)]
#[argx(name = "completions")]
struct Cli {
    /// Shell whose completion adapter should be generated.
    #[argx(value_enum)]
    shell: Shell,
}

fn main() {
    let cli = Cli::parse();
    let script = match Cli::render_completion(cli.shell) {
        Ok(script) => script,
        Err(error) => {
            eprintln!("{error}");
            std::process::exit(1);
        }
    };

    if let Err(error) = io::stdout().lock().write_all(script.as_bytes()) {
        eprintln!("failed to write completion script: {error}");
        std::process::exit(1);
    }
}