Crate cli_command

Crate cli_command 

Source
Expand description

§cli-command

A lightweight and ergonomic command-line argument parser for Rust applications.

§Features

  • 🚀 Minimal dependencies - Only 3 lightweight dependencies for macro support
  • 🎯 Dual API design - Both method-based and macro-based interfaces
  • 🔧 Flexible parsing - Supports both - and -- argument prefixes
  • 📝 Type conversion - Built-in support for common types
  • Error handling - Comprehensive error types with helpful messages
  • 🧪 Well tested - Extensive test coverage
  • 🎨 Macro ergonomics - cli_args! macro for boilerplate-free argument extraction
  • 🎭 Command matching - cli_match! macro for clean command routing

§Quick Start

use cli_command::{parse_command_line, Command};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Parse command line arguments
    let cmd = parse_command_line().unwrap();
     
    // Get a simple argument
    if let Some(port) = cmd.get_argument("port") {
        println!("Port: {}", port);
    }
     
    // Get a required argument with type conversion
    let threads: usize = cmd.get_argument_or_default("threads", 4).unwrap();
    println!("Threads: {}", threads);
     
    // Get argument with default value
    let timeout: u64 = cmd.get_argument_or_default("timeout", 30).unwrap();
    println!("Timeout: {}", timeout);
     
    Ok(())
}

§Examples

See the examples/ directory for complete working examples:

  • simple_server.rs - A web server with configuration options
  • file_processor.rs - A file processing tool with multiple subcommands

§Error Handling

The crate provides comprehensive error handling with helpful error messages:

use cli_command::{CliError, CliErrorKind};

use cli_command::parse_command_string;
let cmd = parse_command_string("--required_arg value").unwrap();
match cmd.get_argument_mandatory("required_arg") {
    Ok(value) => println!("Got: {}", value),
    Err(CliError { kind: CliErrorKind::MissingArgument(arg), .. }) => {
        eprintln!("Missing required argument: {}", arg);
    }
    Err(e) => eprintln!("Error: {}", e),
}

Re-exports§

pub use cli_error::from_error;
pub use cli_error::CliError;
pub use cli_error::CliErrorKind;
pub use command::Command;
pub use parse::parse_command_line;
pub use parse::parse_command_string;

Modules§

cli_error
command
parse

Macros§

cli_args
Argument extraction macro
cli_match
Command matching macro