pirate/tests/main.rs
Zach Dziura 25327fa441 Now returns an error when not all program args are passed via command-line
There was a lot of stuff added here. The most important addition is
how Pirate now returns a MissingArgument kinded error if a required
program argument isn't passed to the program.
2015-04-30 15:14:34 +00:00

50 lines
1.1 KiB
Rust

extern crate pirate;
use std::env;
use pirate::Matches;
fn main() {
let opts = &["n:", "b/boop", ":input"];
let matches: Matches = match pirate::parse(env::args(), opts) {
Err(ref e) => {
println!("Error: {}", e);
help();
return;
},
Ok(m) => m
};
// Print the program help if necessary
if matches.has_arg("h") || matches.has_arg("help") {
help();
return;
}
let input = matches.get("input").unwrap().parse::<i32>().unwrap();
let num = match matches.get("n") {
Some(n) => n.parse::<i32>().unwrap(),
None => 1
};
let sum = input + num;
println!("{} + {} = {}", input, num, sum);
if matches.has_arg("b") || matches.has_arg("boop") {
println!("Boop!!");
}
}
fn help() {
println!("usage: pirate-test [-n NUM] [-b|--boop] INPUT\n");
println!("Options:");
println!(" -n NUM\tChange the default number that's added to the input");
println!(" -b, --boop\tIt's a surprise!");
println!("\nRequired arguments:");
println!(" INPUT\tWe're gonna manipulate this somehow, you'll see!");
}