Crate sap

Source
Expand description

§Sap - a Small Argument Parser

A minimal, zero-dependency Unix command-line argument parser for Rust.

Sap provides full control over argument parsing with an iterator-based API that handles GNU-style options while maintaining simplicity and flexibility.

§Features

  • GNU-style option parsing: Support for short (-a), long (--verbose), and combined options (-abc)
  • Flexible value handling: Options with values via --name=value or separate arguments
  • POSIX compliance: Handle -- separator and - (stdin) arguments correctly
  • Zero dependencies: Pure Rust implementation with no external crates
  • Iterator-based: Works with any Iterator<Item = Into<String>> for maximum flexibility
  • Comprehensive error handling: Descriptive error messages for invalid input

§Example

use sap::{Parser, Argument};

// Parse from string arrays directly - no need to convert to String first!
let mut parser = Parser::from_arbitrary(["myprogram", "-v", "--file=input.txt"]).unwrap();

while let Some(arg) = parser.forward().unwrap() {
    match arg {
        Argument::Short('v') => println!("Verbose mode enabled"),
        Argument::Long("file") => {
            if let Some(filename) = parser.value() {
                println!("Processing file: {}", filename);
            }
        }
        Argument::Value(val) => println!("Positional argument: {}", val),
        _ => {}
    }
}

Structs§

Parser
A stateful command-line argument parser.

Enums§

Argument
Represents a parsed command-line argument.
ParsingError
Errors that can occur during argument parsing.

Type Aliases§

Result
A Result type alias using ParsingError as the default error type.