🌿 Sap
A small, simple and sweet argument parser for Rust
Sap is a minimal, zero-dependency Unix command-line argument parser for Rust. It 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
Quick Start
Add Sap to your Cargo.toml:
[]
= "0.2.0"
Usage
Basic Example
use ;
// Parse from command line arguments
let mut parser = from_env.unwrap;
while let Some = parser.forward.unwrap
Parsing Custom Arguments
use ;
// Parse from any iterator of string-like values
let mut parser = from_arbitrary.unwrap;
while let Some = parser.forward.unwrap
Argument Types
Sap recognizes four types of arguments:
Argument::Short(char)- Short options like-v,-x, and combined ones like-abcArgument::Long(&str)- Long options like--verbose,--file, including values like--file=foo.txtArgument::Value(Cow<str>)- Positional arguments and operandsArgument::Stdio- The special-argument (stdin/stdout)
Complete Example
Here's an example showing a typical CLI application:
use ;
Real-World Examples
puppyutils is a collection of Unix utilities written in Rust that uses sap as its argument parser. Sap was originally built for that project, so the source is a good reference for real usage.
treesap: derive-macro interface
treesap is a companion crate that lets you
declare your CLI arguments as a plain Rust struct instead of writing a manual
parsing loop. It is built on top of sap and exposes a #[derive(Parser)]
macro that generates a parse() method for you.
treesap is a work in progress. Not all field types and attributes are implemented yet. See the treesap README for the current status.
[]
= "0.2.0"
= "0.1.0"
use Parser;
Acknowledgments
Special thanks to Esther who wrote the original parser design for this library <3
License
This project is licensed under the Apache-2.0 License. For more information, please see the LICENSE file.