Skip to main content

Crate sap

Crate sap 

Source
Expand description

§Sap - a Small Argument Parser

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

Sap exposes an iterator-based API that handles GNU-style options and gives you full control over how each argument is consumed.

§Features

  • GNU-style option parsing: short (-a), long (--verbose), and combined options (-abc)
  • Value handling: options with values via --name=value or as a separate following argument
  • POSIX compliance: -- separator and - (stdin) are handled correctly
  • Zero dependencies: no external crates
  • Iterator-based: works with any iterator yielding ArgLike items (&str, String, OsStr, etc.), so you can parse args from the environment, a Vec, or a test fixture without conversion
  • Error handling: errors carry the offending argument and the parser transitions to a defined poisoned state

For a #[derive(Parser)] interface built on top of this crate, see the companion crate treesap.

§Example

use sap::{Parser, Argument};

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().unwrap() {
                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.

Traits§

ArgLike
Trait for types that can be used as command-line arguments.

Type Aliases§

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