cli-command 0.1.0

A lightweight and ergonomic command-line argument parser for Rust
Documentation
//! A comprehensive example demonstrating the cli_match! macro.
//!
//! This example shows how to use the cli_match! macro for command routing
//! without needing to manually parse the command line or pass the command object.

use cli_command::{cli_args, cli_match};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("🔧 CLI Match Macro Demo");
    println!();

    cli_match! {
        "server" => {
            println!("🚀 Starting server with cli_match! macro...");
            start_server()
        },
        "client" => {
            println!("📡 Starting client with cli_match! macro...");
            start_client()
        },
        "config" => {
            println!("⚙️  Managing configuration with cli_match! macro...");
            manage_config()
        },
        "help" | "--help" | "-h" => {
            print_help();
            Ok(())
        },
        _ => {
            eprintln!("❌ Unknown command");
            print_help();
            Ok(())
        }
    }
}

/// Start a server using both cli_match! and cli_args! macros
fn start_server() -> Result<(), Box<dyn std::error::Error>> {
    let (port, host, workers, ssl, verbose) = cli_args!(
        port: u16 = 8080,
        host: String = "localhost".to_string(),
        workers: usize = 4,
        ssl: bool = false,
        verbose: bool = false
    );

    let cmd = cli_command::parse_command_line()?;
    let config_file = cmd.get_argument("config-file");

    println!("🌐 Server Configuration:");
    println!("   Host: {}", host);
    println!("   Port: {}", port);
    println!("   Workers: {}", workers);
    println!("   SSL: {}", ssl);
    println!("   Verbose: {}", verbose);

    if let Some(config) = config_file {
        println!("   Config file: {}", config);
    }

    println!("✅ Server started successfully on {}:{}", host, port);

    Ok(())
}

/// Start a client using both cli_match! and cli_args! macros
fn start_client() -> Result<(), Box<dyn std::error::Error>> {
    let (timeout, retries, server_url) = cli_args!(
        timeout: u64 = 30,
        retries: u32 = 3,
        server_url: String = "http://localhost:8080".to_string()
    );

    let cmd = cli_command::parse_command_line()?;
    let username = cmd.get_argument("username");
    let password = cmd.get_argument("password");

    println!("📡 Client Configuration:");
    println!("   Server URL: {}", server_url);
    println!("   Timeout: {}s", timeout);
    println!("   Retries: {}", retries);

    if let Some(user) = username {
        println!("   Username: {}", user);
    }

    if let Some(pass) = password {
        println!("   Password: {}", "*".repeat(pass.len()));
    }

    println!("✅ Client connected to server successfully");

    Ok(())
}

/// Manage configuration using both cli_match! and cli_args! macros
fn manage_config() -> Result<(), Box<dyn std::error::Error>> {
    let (action, config_path, backup, force) = cli_args!(
        action: String = "show".to_string(),
        config_path: String = "./config.toml".to_string(),
        backup: bool = true,
        force: bool = false
    );

    println!("⚙️  Configuration Management:");
    println!("   Action: {}", action);
    println!("   Config path: {}", config_path);
    println!("   Backup: {}", backup);
    println!("   Force: {}", force);

    match action.as_str() {
        "show" => {
            println!("📋 Current configuration:");
            println!("   port = 8080");
            println!("   host = \"localhost\"");
            println!("   workers = 4");
        }
        "backup" => {
            println!("💾 Creating backup of configuration...");
            println!("✅ Backup created successfully");
        }
        "restore" => {
            println!("🔄 Restoring configuration from backup...");
            println!("✅ Configuration restored successfully");
        }
        _ => {
            println!("❌ Unknown action: {}", action);
            println!("Available actions: show, backup, restore");
        }
    }

    Ok(())
}

fn print_help() {
    println!("🔧 CLI Match Macro Example");
    println!();
    println!("This example demonstrates the cli_match! macro for command routing");
    println!("and the cli_args! macro for argument parsing from cli-command crate.");
    println!();
    println!("Usage:");
    println!("  cli_match_example <COMMAND> [OPTIONS]");
    println!();
    println!("Commands:");
    println!("  server    Start a server with configurable options");
    println!("  client    Start a client with connection options");
    println!("  config    Manage configuration files");
    println!("  help      Show this help message");
    println!();
    println!("Server options:");
    println!("  --port <PORT>            Server port (default: 8080)");
    println!("  --host <HOST>            Server host (default: localhost)");
    println!("  --workers <COUNT>        Number of worker threads (default: 4)");
    println!("  --ssl                    Enable SSL/TLS (default: false)");
    println!("  --verbose                Enable verbose logging (default: false)");
    println!("  --config-file <FILE>     Configuration file path (optional)");
    println!();
    println!("Client options:");
    println!("  --server-url <URL>       Server URL (default: http://localhost:8080)");
    println!("  --timeout <SECONDS>      Connection timeout (default: 30)");
    println!("  --retries <COUNT>        Number of retry attempts (default: 3)");
    println!("  --username <USER>        Username for authentication (optional)");
    println!("  --password <PASS>        Password for authentication (optional)");
    println!();
    println!("Config options:");
    println!("  --action <ACTION>        Action to perform: show, backup, restore (default: show)");
    println!("  --config-path <PATH>     Path to configuration file (default: ./config.toml)");
    println!("  --backup                 Create backup before changes (default: true)");
    println!("  --force                  Force operation without confirmation (default: false)");
    println!();
    println!("Examples:");
    println!("  # Start server with custom port and SSL");
    println!("  cargo run --example cli_match_example -- server --port 3000 --ssl --verbose");
    println!();
    println!("  # Start client with authentication");
    println!("  cargo run --example cli_match_example -- client --server-url https://api.example.com --username admin --password secret");
    println!();
    println!("  # Show current configuration");
    println!("  cargo run --example cli_match_example -- config --action show");
    println!();
    println!("  # Backup configuration");
    println!("  cargo run --example cli_match_example -- config --action backup --config-path /etc/myapp.conf");
    println!();
    println!("🔍 Notice how cli_match! eliminates the need to manually parse command line");
    println!("   and cli_args! eliminates boilerplate argument extraction code!");
}