2 releases

✓ Uses Rust 2018 edition

new 0.1.1 Jun 11, 2019
0.1.0 Jun 5, 2019

#93 in Command-line interface

5 downloads per month

MIT/Apache

25KB
474 lines

Capture the Flag   Latest Version

Capture the Flag is a command-line flag parser Rust library.


Usage

use ctflag::{Flags, FromArg, FromArgError, FromArgResult};

#[derive(Flags)]
struct MyFlags {
    #[flag(desc = "The floopy floops the whoop")]
    enable_floopy: bool,

    #[flag(
        desc = "How many slomps to include",
        placeholder = "INTEGER",
        default = 34
    )]
    slomp_count: i64,

    #[flag(desc = "An optional path to a Gmup", placeholder = "PATH")]
    gmup: Option<String>,

    #[flag(short = 'h', desc = "Prints this help message")]
    help: bool,
}

// Custom type.
enum Fruit {
    Apple,
    Orange,
}

impl FromArg for Fruit {
    fn from_arg(s: &str) -> FromArgResult<Self> {
        match s {
            "apple" => Ok(Fruit::Apple),
            "orange" => Ok(Fruit::Orange),
            _ => Err(FromArgError::with_message("must be an apple or orange")),
        }
    }
}

fn main() {
    let result = MyFlags::from_args(std::env::args());
    match result {
        Ok((flags, args)) => {
            if flags.help {
                println!("{}", MyFlags::description());
                return;
            }
            // ...
        }
        Err(err) => {
            println!("Error parsing flags: {}", err);
            println!("{}", MyFlags::description());
        }
    }
}

Setup

[dependencies]
ctflag = "0.1"

Dependencies

~1.5MB
~23K SLoC