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.
Example:
use SCLP::*;
./app --name user1 --h 1.82 --happy
Usage:
String argument:
To get a string type argument you will use strFlag.
Example:
let name = SCLPnew.setDefault.setHelp;
./app --name user
This 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>.
Integer argument:
To get an int type argument you will use intFlag.
Example:
let age = SCLPnew.setDefault.setHelp;
./app --age 31
This 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>.
Float argument:
To get a float type argument you will use floatFlag.
Example:
let height = SCLPnew.setDefault.setHelp;
./app --height 1.73
This 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>.
Boolean argument:
To get a boolean type argument you will use boolFlag.
Example:
let happy = SCLPnew.setHelp;
./app --happy # isHappy is true
./app # isHappy is false
This 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() VS .tryParse():
.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:
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.
Flag format:
Every flag should start with --.Any flag not starting with -- is ignored.
Example:
use SCLP::*;
And in the terminal:
./app --name user1
For bool type of argument:
./app --boolArg
will be returned as true otherwise false or default value.
Help message:
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.
Notice:
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.