SCLP is very simple lightweight rust command line parser with Zero external dependencies.
SCLP was built as learning project for me to learn the programing language Rust.
SCLP is inspired by the Golang flag package.
use SCLP::*;
fn main() {
let name = SCLP::strFlag::new("name")
.setDefault("john".to_string())
.setHelp("Your name.");
let age = SCLP::intFlag::new("age")
.setDefault(22)
.setHelp("Your age.");
// height here is required
let height = SCLP::floatFlag::new("h")
.setHelp("Your height.");
let happy = SCLP::boolFlag::new("happy")
.setHelp("Are you happy.");
println!(
"name: {} age: {} height: {} happy: {}",
name.parse(),
age.parse(),
height.parse(),
happy.parse()
);
}./app --name user1 --h 1.82 --happyTo get a string type argument you will use strFlag.
let name = SCLP::strFlag::new("name").setDefault("john".to_string()).setHelp("Your name.");./app --name userThis will return a strFlag object, and you can use one of two parsing options:
Calling .parse():
let nameValue = name.parse();This will return String.
Calling .tryParse():
let nameValue = name.tryParse();This will return Result<String,ArgumentError>.
To get an int type argument you will use intFlag.
let age = SCLP::intFlag::new("age").setDefault(22).setHelp("Your age.");./app --age 31This will return a intFlag object, and you can use one of two parsing options:
Calling .parse():
let ageValue = age.parse();This will return i32.
Calling .tryParse():
let ageValue = age.tryParse();This will return Result<i32,ArgumentError>.
To get a float type argument you will use floatFlag.
let height = SCLP::floatFlag::new("height").setDefault(1.82).setHelp("Your height in meters.");./app --height 1.73This will return a floatFlag object, and you can use one of two parsing options:
Calling .parse():
let heightValue = height.parse();This will return f32.
Calling .tryParse():
let heightValue = age.heightParse();This will return Result<f32,ArgumentError>.
To get a boolean type argument you will use boolFlag.
let happy = SCLP::boolFlag::new("height").setHelp("Are you happy.");./app --happy # isHappy is true
./app # isHappy is falseThis will return a boolFlag object, and you can use one of two parsing options:
Calling .parse():
let isHappy = happy.parse();This will return f32.
Calling .tryParse():
let isHappy = happy.tryParse();This will return Result<bool,ArgumentError>.
.parse() returns the primitive types + String and will exit in the case of marking the argument as required (by not
setting default value with .setDefault()) and the user not providing the argument.
.tryParse() returns a Result<T,ArgumentError> and let you decide what will happen in the case of:
ArgumentError is an enum contain the following values:
ARG_REQUIREDwill be returned when the value is required and the user didn't provide it.ARG_PROCESSINGwill be returned when you are trying to parse ai32orf32type of argument but the user provided aString.
Every flag should start with --.Any flag not starting with -- is ignored.
use SCLP::*;
fn main() {
let name = SCLP::strFlag::new("name")
.setDefault("john".to_string())
.setHelp("Your name.");
println!("name: {}", name.parse());
}And in the terminal:
./app --name user1For bool type of argument:
./app --boolArgwill be returned as true otherwise false or default value.
The library creates a default help message that will be shown when the user uses --help flag.
Currently there are no way to change the help message.
This library is for learning purposes only I am planning to make future updates for it as I get better at rust but nothing to promis.