Skip to content

CipherDay/SCLP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SCLP

📦 Simple Command Line Parser


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::*;

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 --happy

Usage:

String argument:

To get a string type argument you will use strFlag.

Example:

let name = SCLP::strFlag::new("name").setDefault("john".to_string()).setHelp("Your name.");
./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 = SCLP::intFlag::new("age").setDefault(22).setHelp("Your age.");
./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 = SCLP::floatFlag::new("height").setDefault(1.82).setHelp("Your height in meters.");
./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 = SCLP::boolFlag::new("height").setHelp("Are you happy.");
./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_REQUIRED will be returned when the value is required and the user didn't provide it.
  • ARG_PROCESSING will be returned when you are trying to parse a i32orf32 type of argument but the user provided a String.

Flag format:

Every flag should start with --.Any flag not starting with -- is ignored.

Example:

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 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.

About

Simple Command Line Parser Built with Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages