Created
February 13, 2012 22:30
-
-
Save mcandre/1821011 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std; | |
import std::getopts::*; | |
import result::*; | |
fn do_work(in: str, out: option<str>) { | |
// ... | |
} | |
fn print_usage(program: str) { | |
std::io::println("Usage: " + program + " [options]"); | |
std::io::println("-o\t\tOutput"); | |
std::io::println("-h --help\tUsage"); | |
} | |
fn main(args: [str]) { | |
check vec::is_not_empty(args); | |
let program : str = vec::head(args); | |
let opts = [ | |
optopt("o"), | |
optflag("h"), | |
optflag("help") | |
]; | |
let match = alt getopts(vec::tail(args), opts) { | |
ok(m) { m } | |
err(f) { fail fail_str(f) } | |
}; | |
if opt_present(match, "h") || opt_present(match, "help") { | |
print_usage(program); | |
ret; | |
} | |
let output = opt_maybe_str(match, "o"); | |
let input = if !vec::is_empty(match.free) { | |
match.free[0] | |
} else { | |
print_usage(program); | |
ret; | |
}; | |
do_work(input, output); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment