Capture the Flag is a command-line flag parser Rust library.
```rust use ctflag::{Flags, FromArg, FromArgError, FromArgResult};
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 fromarg(s: &str) -> FromArgResult
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()); } } } ```
toml
[dependencies]
ctflag = "0.1"