1 unstable release
0.1.0 | Jan 11, 2025 |
---|
#40 in #minimalist
44KB
988 lines
Parsin
Parsin is a minimalistic Command Line Interface parser for Rust. Parsin offers a simple way to creating context to parse that is both fast and simple.
Getting Started
Add parsin into your cargo project by using this command
cargo add parsin
Once the crate has been added include these imports
use parsin::{
Context,
Type,
parse
};
Initialize Context
#
let context = Context::from((
&[
("Argument", Type::Str, "test argument desc", true, Some("string default value"))
],
&[
("--flag", Type::Bool, "test flag desc", false, None)
]
));
Pass a reference of Context
into parse
.
use parsin::parser::ParsedArguments;
#
// Final object
// contains `.flags` and `.arguments` which contain
// hashmaps whose values are Value (enum@crate::parser::Value)
let parsed: ParsedArguments = parse(&context);
Working with Parsin
Regarding cargo projects, if you wish to pass arguments during the
building process, when you run cargo run --release
or cargo run
,
you can insert --
between the cargo command and your arguments.
Take the following for example:
cargo run --release -- {ARGUMENT} {FLAG1} {FLAG2}
Or
cargo run -- {ARGUMENT} {FLAG1} {FLAG2}
Examples
Initiating Context
use parsin::{Context, Type};
let ctx = Context::from(( // within a tuple
&[ // First list defines the arguments
("name", Type::Str, "Your name", true, None), // Arg
],
&[ // Second list defines the flags
("--repeat", Type::Int, "The amount of times to greet", false, Some("1")), // Flag
]
));
Alternatively, a more manual approach would include the usage of [Arg
] and Flag
.
use parsin::{Context, Type, Flag, Arg};
let ctx = Context::from((
&[
Arg::from(("arg1", Type::Str, "argument one", true, None)),
Arg::from(("arg2", Type::Str, "argument two", true, None)),
],
&[
Flag::from(("flag1", Type::Int, "flag with int value", false, Some("19"))),
Flag::from(("flag2", Type::Bool, "once raised, returns true for this flag", false, Some("false"))),
]
));