cli-command 0.1.0

A lightweight and ergonomic command-line argument parser for Rust
Documentation
//! A simple HTTP server example using cli-command for argument parsing.
//!
//! This example demonstrates how to use cli-command to parse command line arguments
//! for a web server with various configuration options.

use cli_command::{parse_command_line, Command};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

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

    match cmd.name.as_str() {
        "serve" => {
            println!("Starting HTTP server...");
            start_server(&cmd)?;
        }
        "help" | "--help" | "-h" => {
            print_help();
        }
        _ => {
            println!("Unknown command: {}", cmd.name);
            print_help();
        }
    }

    Ok(())
}

fn start_server(cmd: &Command) -> Result<(), Box<dyn std::error::Error>> {
    let host: IpAddr = cmd.get_argument_or_default("host", Ipv4Addr::new(127, 0, 0, 1).into())?;
    let port: u16 = cmd.get_argument_or_default("port", 8080)?;
    let workers: usize = cmd.get_argument_or_default("workers", 4)?;
    let max_connections: usize = cmd.get_argument_or_default("max-connections", 1000)?;

    let enable_ssl = cmd.contains_argument("ssl");
    let enable_compression = cmd.contains_argument("compression");
    let verbose = cmd.contains_argument("verbose") || cmd.contains_argument("v");

    let config_file = cmd.get_argument("config");

    let addr = SocketAddr::new(host, port);

    println!("Server configuration:");
    println!("  Address: {}", addr);
    println!("  Workers: {}", workers);
    println!("  Max connections: {}", max_connections);
    println!("  SSL enabled: {}", enable_ssl);
    println!("  Compression enabled: {}", enable_compression);
    println!("  Verbose logging: {}", verbose);

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

    println!("\nServer would start on {} with {} workers", addr, workers);

    Ok(())
}

fn print_help() {
    println!("Simple HTTP Server");
    println!();
    println!("Usage:");
    println!("  server serve [OPTIONS]");
    println!("  server help");
    println!();
    println!("Commands:");
    println!("  serve    Start the HTTP server");
    println!("  help     Show this help message");
    println!();
    println!("Options:");
    println!("  --host <IP>              Server host (default: 127.0.0.1)");
    println!("  --port <PORT>            Server port (default: 8080)");
    println!("  --workers <COUNT>        Number of worker threads (default: 4)");
    println!("  --max-connections <NUM>  Maximum concurrent connections (default: 1000)");
    println!("  --ssl                    Enable SSL/TLS");
    println!("  --compression            Enable response compression");
    println!("  --config <FILE>          Configuration file path");
    println!("  --verbose, -v            Enable verbose logging");
    println!();
    println!("Examples:");
    println!("  server serve --port 3000 --workers 8");
    println!("  server serve --host 0.0.0.0 --port 80 --ssl");
    println!("  server serve --config /etc/server.conf --verbose");
}