cli-command 0.1.0

A lightweight and ergonomic command-line argument parser for Rust
Documentation
//! A file processing tool example using cli-command for argument parsing.
//!
//! This example demonstrates how to use cli-command to parse command line arguments
//! for a file processing tool with multiple subcommands and various options.

use cli_command::{parse_command_line, Command};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cmd = parse_command_line()?;

    match cmd.name.as_str() {
        "compress" => {
            println!("Compressing files...");
            compress_files(&cmd)?;
        }
        "extract" => {
            println!("Extracting files...");
            extract_files(&cmd)?;
        }
        "list" => {
            println!("Listing files...");
            list_files(&cmd)?;
        }
        "help" | "--help" | "-h" => {
            print_help();
        }
        _ => {
            println!("Unknown command: {}", cmd.name);
            print_help();
        }
    }

    Ok(())
}

fn compress_files(cmd: &Command) -> Result<(), Box<dyn std::error::Error>> {
    let input_files = cmd.get_argument_mandatory_all("input")?;
    let output_file = cmd.get_argument_mandatory("output")?;

    let compression_level: u8 = cmd.get_argument_or_default("level", 6)?;
    let algorithm = cmd.get_argument_or_default("algorithm", "gzip".to_string())?;
    let recursive = cmd.contains_argument("recursive") || cmd.contains_argument("r");
    let verbose = cmd.contains_argument("verbose") || cmd.contains_argument("v");

    println!("Compression settings:");
    println!("  Input files: {:?}", input_files);
    println!("  Output file: {}", output_file);
    println!("  Compression level: {}", compression_level);
    println!("  Algorithm: {}", algorithm);
    println!("  Recursive: {}", recursive);
    println!("  Verbose: {}", verbose);

    println!(
        "\nCompressing {} files to {}...",
        input_files.len(),
        output_file
    );

    Ok(())
}

fn extract_files(cmd: &Command) -> Result<(), Box<dyn std::error::Error>> {
    let archive_file = cmd.get_argument_mandatory("archive")?;
    let output_dir = cmd.get_argument_or_default("output-dir", ".".to_string())?;
    let overwrite = cmd.contains_argument("overwrite");
    let verbose = cmd.contains_argument("verbose") || cmd.contains_argument("v");

    println!("Extraction settings:");
    println!("  Archive file: {}", archive_file);
    println!("  Output directory: {}", output_dir);
    println!("  Overwrite existing: {}", overwrite);
    println!("  Verbose: {}", verbose);

    println!("\nExtracting {} to {}...", archive_file, output_dir);

    Ok(())
}

fn list_files(cmd: &Command) -> Result<(), Box<dyn std::error::Error>> {
    let archive_file = cmd.get_argument_mandatory("archive")?;
    let detailed = cmd.contains_argument("detailed") || cmd.contains_argument("l");
    let verbose = cmd.contains_argument("verbose") || cmd.contains_argument("v");

    println!("Listing files in: {}", archive_file);
    println!("Detailed view: {}", detailed);
    println!("Verbose: {}", verbose);

    println!("\nArchive contents:");
    println!("  file1.txt (1024 bytes)");
    println!("  file2.txt (2048 bytes)");
    println!("  subdir/file3.txt (512 bytes)");

    Ok(())
}

fn print_help() {
    println!("File Processor Tool");
    println!();
    println!("Usage:");
    println!("  processor <COMMAND> [OPTIONS]");
    println!();
    println!("Commands:");
    println!("  compress    Compress files into an archive");
    println!("  extract     Extract files from an archive");
    println!("  list        List files in an archive");
    println!("  help        Show this help message");
    println!();
    println!("Compress options:");
    println!("  --input <FILE>...        Input files to compress (required)");
    println!("  --output <FILE>          Output archive file (required)");
    println!("  --level <0-9>            Compression level (default: 6)");
    println!("  --algorithm <NAME>       Compression algorithm (default: gzip)");
    println!("  --recursive, -r          Process directories recursively");
    println!();
    println!("Extract options:");
    println!("  --archive <FILE>         Archive file to extract (required)");
    println!("  --output-dir <DIR>       Output directory (default: current)");
    println!("  --overwrite              Overwrite existing files");
    println!();
    println!("List options:");
    println!("  --archive <FILE>         Archive file to list (required)");
    println!("  --detailed, -l           Show detailed file information");
    println!();
    println!("Common options:");
    println!("  --verbose, -v            Enable verbose output");
    println!("  --help, -h               Show this help message");
    println!();
    println!("Examples:");
    println!("  processor compress --input file1.txt file2.txt --output archive.gz");
    println!("  processor extract --archive archive.gz --output-dir ./extracted");
    println!("  processor list --archive archive.gz --detailed");
}