Skip to content

Instantly share code, notes, and snippets.

@mcandre
Created February 13, 2012 22:30
Show Gist options
  • Save mcandre/1821011 to your computer and use it in GitHub Desktop.
Save mcandre/1821011 to your computer and use it in GitHub Desktop.
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