7 releases (stable)

2.0.0 Feb 25, 2019
1.3.0 Feb 23, 2019
0.1.0 Feb 18, 2019

#789 in Command-line interface

Apache-2.0

46KB
178 lines

Arg Soup

Arg soup is a super simple command line argument parser for rust.

It follows a very simple principle of "action args" and "flag args"

An action arg is simply an argument that causes the program to do something. For example: if someone runs your program with "--help", you would want to print something out. You would do this with an action arg.

Quick use:

Add arg-soup = "*" to the dependancies section on your Cargo.toml file.

Why use arg-soup?

Arg soup is extremely simple an small. All of the functionality of the crate can be accessed by bringing a single sruct (Parser) into scope, then using its provided methods. All of the functions is intuitive and very quick to use, meaning you can become familiar with every function in a few minutes!

Also, arg soup is being constantly updated and improved. And, new features will usually be added on request!

Action arg in action:
use arg_soup::Parser;
use std::env;

fn main() {
    // Collect all of the arguments from the environment
    let args: Vec<String> = env::args().collect();
    // Create a new parser with these arguments
    let mut parser = Parser::new(args);
    // Add an action arg and associate it with an action
    parser.add_action("--help", Box::new(|| println!("This is an example of my crate")));
    parser.execute_actions();
}

With the above code, when the program is run with "--help", println!("This is an example of my crate") will be executed.

Flag args are just as simple. They collect some value from the command line. For example, say you wanted to get the path of some file. You would do this with a flag arg by running the program with "-o file.txt".

Flag arg in action:

use arg_soup::Parser;
use std::env;

fn main() {
    // Collect all of the arguments from the environment
    let args: Vec<String> = env::args().collect();
    // Create a new parser with these arguments
    let mut parser = Parser::new(args);
    // Create a flag arg and have it collect values
    parser.add_flag("-o");
    let flags = parser.collect_flags();
    println!("{:?}", flags);
}

The above will return a hashmap of the argument mapped to the value.

These can of course be combined as much as needed:

use arg_soup::Parser;
use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
    let mut parser = Parser::new(args);
    parser.add_action("--help", Box::new(|| println!("Some text")));
    parser.add_action("--cats", Box::new(|| println!("Meow")));
    parser.add_flag("-o");
    parser.add_flag("-z");
    
    // `actions` will hold the name of all executed arguments
    let actions = parser.execute_actions();
    let flags = parser.collect_flags();
    println!("{:?}", flags);
}

Other methods

// Will return true if argument is present and false if not
let b = parser.get_bool("--test");
// Will return Some(&str) if it's value is present, and None if not
let v = parser.get_val("-o");

TODO:

Implement CustomAction trait

That's it! All the functions you saw above are the functions avaliable for you to use. This 'library' is microscopic, just designed to make your time a little easier.

Some example outputs when running the code found in the example file:

No runtime deps