| Crates.io | simpleargumentsparser |
| lib.rs | simpleargumentsparser |
| version | 2.1.0 |
| created_at | 2025-12-19 04:54:39.020078+00 |
| updated_at | 2025-12-19 04:54:39.020078+00 |
| description | A simple command line argument parser with colors |
| homepage | |
| repository | https://github.com/stringmanolo/simpleargumentsparser |
| max_upload_size | |
| id | 1994236 |
| size | 29,708 |
Add to your Cargo.toml:
[dependencies]
simpleargumentsparser = "2.1.0"
use simpleargumentsparser::parse_cli;
use std::process;
fn main() {
let cli = parse_cli();
let mut verbose = false;
if cli.no_args {
exit("Arguments needed");
}
if cli.s.contains_key("h") || cli.c.contains_key("help") {
exit("Help Menu:\n\nThis is just an example");
}
if cli.s.contains_key("v") || cli.c.contains_key("verbose") {
verbose = true;
}
if cli.c.contains_key("version") {
exit("V0.0.1");
}
if cli.s.contains_key("s") {
println!("Hello!");
}
if let Some(p) = &cli.p {
println!("Hello {}", p);
}
if cli.c.contains_key("debug-arguments") {
println!("{:#?}", cli);
}
if verbose {
println!("Verbose mode is on");
}
}
fn exit(msg: &str) {
println!("{}", msg);
process::exit(0);
}
The Rust version features a fluent builder pattern for color styling:
use simpleargumentsparser::parse_cli;
fn main() {
let cli = parse_cli();
// Basic colors with direct application
println!("{}", cli.color.red("Error message"));
println!("{}", cli.color.green("Success!"));
println!("{}", cli.color.blue("Information"));
// Chaining styles
println!("{}", cli.color.bold().red("Bold red text"));
println!("{}", cli.color.underline().yellow("Underlined yellow"));
println!("{}", cli.color.bold().italic().cyan("Bold italic cyan"));
// Bright colors
println!("{}", cli.color.bright_red("Bright red"));
println!("{}", cli.color.bright_green("Bright green"));
// Background colors with chaining
println!("{}", cli.color.bg_red().white("White text on red background"));
println!("{}", cli.color.bg_blue().bright_white("Bright white on blue"));
// Using paint() for explicit styling
println!("{}", cli.color.bold().underline().paint("Explicitly painted text"));
}
color! MacroFor more concise color combinations, use the color! macro:
use simpleargumentsparser::color;
fn main() {
// Combine multiple styles in one line
println!("{}", color!(bold red "Bold red text"));
println!("{}", color!(underline yellow "Underlined yellow"));
println!("{}", color!(bg_blue bright_white "White on blue background"));
println!("{}", color!(bold italic cyan "Bold italic cyan"));
println!("{}", color!(bg_red white blink "Blinking white on red"));
}
Run the built-in color showcase:
cargo run --example colored -- --showcase
cargo run --example colored -- --all
cargo run --example colored -- --help
pub struct CLI {
pub s: HashMap<String, String>, // Single-dash arguments (-v, -h)
pub c: HashMap<String, String>, // Double-dash arguments (--verbose, --help)
pub o: Vec<(String, usize)>, // Positional arguments with indices
pub p: Option<String>, // Piped input (None if no pipe)
pub e: Vec<usize>, // Error indices
pub no_args: bool, // True if no arguments provided
pub argc: usize, // Argument count
pub color: Color, // ANSI color system with builder pattern
}
# Basic example with arguments
cargo run --example basic -- -v --debug-arguments
# Colored example showcase
cargo run --example colored -- --showcase
# Specific color demonstrations
cargo run --example colored -- --colors
cargo run --example colored -- --bright
cargo run --example colored -- --backgrounds
# The example binary
cargo run --bin cli-example -- -h
cargo run --bin cli-example -- --version
# Using echo
echo "Hello from pipe" | cargo run --example basic
# Using cat with a file
cat Cargo.toml | cargo run --example basic -- --debug-arguments
# Combined with arguments
printf "Line 1\nLine 2" | cargo run --bin cli-example -- -v
color! macro for concise stylingblack(text: &str) -> Stringred(text: &str) -> Stringgreen(text: &str) -> Stringyellow(text: &str) -> Stringblue(text: &str) -> Stringmagenta(text: &str) -> Stringcyan(text: &str) -> Stringwhite(text: &str) -> Stringbright_black(text: &str) -> Stringbright_red(text: &str) -> Stringbright_green(text: &str) -> Stringbright_yellow(text: &str) -> Stringbright_blue(text: &str) -> Stringbright_magenta(text: &str) -> Stringbright_cyan(text: &str) -> Stringbright_white(text: &str) -> Stringbold() -> Selfdim() -> Selfitalic() -> Selfunderline() -> Selfblink() -> Selfinvert() -> Selffg_black() -> Selffg_red() -> Selffg_green() -> Selffg_yellow() -> Selffg_blue() -> Selffg_magenta() -> Selffg_cyan() -> Selffg_white() -> Selfbg_black() -> Selfbg_red() -> Selfbg_green() -> Selfbg_yellow() -> Selfbg_blue() -> Selfbg_magenta() -> Selfbg_cyan() -> Selfbg_white() -> Selfapply(text: &str) -> Stringpaint(text: &str) -> String (alias for apply)use simpleargumentsparser::parse_cli;
use std::process;
fn main() {
let cli = parse_cli();
// Help with styled output
if cli.s.contains_key("h") || cli.c.contains_key("help") {
println!("{}", cli.color.bold().cyan("My Rust CLI"));
println!("{}", cli.color.dim().paint("Version 1.0.0"));
println!("\n{}", cli.color.bold().paint("Usage:"));
println!(" myapp [options]");
println!("\n{}", cli.color.bold().paint("Options:"));
println!(" {} Show this help", cli.color.green("-h, --help"));
println!(" {} Verbose output", cli.color.green("-v, --verbose"));
println!(" {} Show version", cli.color.green("--version"));
process::exit(0);
}
// Status messages with icons
println!("{} {}", cli.color.green("✓"), cli.color.bold().paint("Operation successful"));
println!("{} {}", cli.color.red("✗"), "Something went wrong");
println!("{} {}", cli.color.yellow("⚠"), "Warning: Deprecated feature");
// Formatted output with chained styles
let username = "John";
println!("Hello, {}!", cli.color.bold().yellow(username));
// Code highlighting
println!("{} {} = {};",
cli.color.magenta("let"),
cli.color.cyan("count"),
cli.color.green("42")
);
}