yaccas

Crates.ioyaccas
lib.rsyaccas
version0.2.1
sourcesrc
created_at2016-08-10 12:21:51.139684
updated_at2016-12-22 12:19:17.708036
descriptionYet Another Callback-orientated Command line pArSer - nomen est omen.
homepagehttps://github.com/Christopher22/yaccas.git
repositoryhttps://github.com/Christopher22/yaccas.git
max_upload_size
id5938
size33,106
Christopher Gundler (Christopher22)

documentation

https://christopher22.github.io/yaccas/yaccas/

README

Yaccas Build Status

Yet Another Callback-orientated Command line pArSer is ... well, yet another command line parser.

Features or "Yet another?! You are not that creative, are you?!"

Indeed, there are so many command line parser written in Rust out there... But would I have written this one if it is like all the other? Let me convince you: Why should you choose this one?

  • Smoothly to integrate into existing projects
  • Extreme lightweight, easy & fast
    • Zero dependencies: Only pure Rust!
  • No handler, references or other bullshit: Just modern callbacks!
  • Ready for every system: Accept the syntax of UNIX exactly like that one of WINDOWS.
  • Completely documented, many (doc-)tests to check correctness

Documentation

Documentation on GitHub

Example

There are two ways to use Yaccas: The preferred callback-oriented method or a argument-oriented one.

#[macro_use]
extern crate yaccas;

use yaccas::arguments::{Argument, Command, Flag, Value};
use yaccas::parser::{Parser, FreeArgumentSupport, Result};

fn method_with_callback() {
    let mut will_be_true_if_flag_is_set = false;
    let mut will_be_42_as_everytime = 0u32;
    
    { // It's time for some magic ...
    
        // There a three types of arguments
        let flag = Flag::default();
        let value = Value::new::<u32>();
        let command = Command::new(|| Some("A fancy name for abort"));
        
        // Registers the arguments to a parser.
        // All callbacks will only be executed if parsing was successful!
        let mut parser = Parser::default();
        
        parser.register(&["option", "o1", "o2"], Argument::with_callback(flag, | flag | {
            // Flags are options which may occur 0 - x times.
            will_be_true_if_flag_is_set = flag.is_activated();
        }));
        
        parser.register(&["value", "v"], Argument::with_callback(value, | value | {
            // Values are command line argument-value pairs of a specific type.
            will_be_42_as_everytime = value.get_value::<u32>().expect("The answer for everything is 42!");
        }));

        parser.register(&["abort"], Argument::with_callback(command, | _command | {
            // Commands may or may not abort the execution of parsing, i.e. for "help".
            // This callback is a fallback: It is only called if the process was not aborted! 
        }));
        
        match parser.parse(default_scanner!()) {
            Result::Success(free_arguments) => { /* ... */ },
            Result::Aborted("A fancy name for abort") => { /* ... */ },
            _ => { /* ... */ }
        }
    }
    
    // Do something with "will_be_true_if_flag_is_set" or "will_be_42_as_everytime" here ...
}

fn method_without_callback() {
    
    let mut flag = Flag::default();
    let mut value = Value::new::<u32>();
    let mut command = Command::new(|| Some("A fancy name for abort"));
            
    { // It's time for some magic ...
        // Registers the arguments to a parser.
        let mut parser = Parser::default();
        
        parser.register(&["option", "o1", "o2"], flag);
        parser.register(&["value", "v"], value);
        parser.register(&["abort"], command);
        
        match parser.parse(default_scanner!()) {
            Result::Success(free_arguments) => { /* ... */ },
            Result::Aborted("A fancy name for abort") => { /* Abort execution */ },
            _ => { /* Abort execution */ }
        }
    }
    
    // Access arguments directly here to access i.e. their values.
}

##Author Christopher Gundler (c.gundler@mail.de)

##License Licensed under either of

at your option.

##Contribution Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 26

cargo fmt