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=valueor 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
ArgLikeitems (&str,String,OsStr, etc.), so you can parse args from the environment, aVec, 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.
- Parsing
Error - Errors that can occur during argument parsing.
Traits§
- ArgLike
- Trait for types that can be used as command-line arguments.
Type Aliases§
- Result
- A
Resulttype alias usingParsingErroras the default error type.