| Crates.io | commandargs |
| lib.rs | commandargs |
| version | 0.1.5 |
| created_at | 2025-07-17 14:24:43.454516+00 |
| updated_at | 2025-07-19 18:14:38.929059+00 |
| description | Minimal, precise library for primarily fetching and sorting runtime command line arguments accordingly. |
| homepage | |
| repository | https://github.com/NotFlawffles/commandargs |
| max_upload_size | |
| id | 1757573 |
| size | 29,311 |
Minimal, precise library for primarily fetching and sorting runtime command line arguments accordingly.
For each command line argument will be rawly read, based on a user-specified command pattern
it will check if it was expected, then it will ensure that n (user-defined) amount of
arguments will be read (order between arguments and options is arbitrary), at the same time
it will also be reading options based on user-specified set of option patterns.
use crate::{args::Args, command::{command_pattern::CommandPattern, Command}, option::option_pattern::{ArgumentedOptPatArg, OptionPattern}};
fn main() {
let command = Command::from_args(
Args::CommandLineArgs,
&[
CommandPattern::new(
"greet",
1,
&[
OptionPattern::Argumented(
"method",
ArgumentedOptPatArg::Specific(&["hello", "hi"]),
),
OptionPattern::Argumented("also-greet", ArgumentedOptPatArg::Any),
],
&|args, opts| {
// implementation of "greet" command
let name = args.get(0).unwrap();
let method = opts.get_argumented("method").map_or("hello", |method| method);
let also_greet = opts.get_argumented("also-greet");
println!("{method} {name}!");
if let Some(name) = also_greet {
println!("and {method} {name}!");
}
},
), /* you can also add more commands with the same logic and try other options */
],
);
if let Err(message) = command {
eprintln!("{message}");
return;
}
let command = command.unwrap();
command.execute();
}
Result (the -- is required to prevent further options from passing into cargo):
$ cargo run -- greet NotFlawffles -method hi -also-greet "John Doe"
hi NotFlawffles!
and hi John Doe!