Skip to content

Commit 1a19309

Browse files
committedMay 1, 2012
std: Fix example in getopts module docs
Issue #1833.
1 parent 22254c3 commit 1a19309

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed
 

‎src/libstd/getopts.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,45 @@ The following example shows simple command line parsing for an application
1919
that requires an input file to be specified, accepts an optional output file
2020
name following -o, and accepts both -h and --help as optional flags.
2121
22+
use std;
23+
import std::getopts::{optopt,optflag,getopts,opt_present,opt_maybe_str,
24+
fail_str};
25+
26+
fn do_work(in: str, out: option<str>) {
27+
// ...
28+
}
29+
30+
fn print_usage(program: str) {
31+
io::println(\"Usage: \" + program + \" [options]\");
32+
io::println(\"-o\t\tOutput\");
33+
io::println(\"-h --help\tUsage\");
34+
}
35+
2236
fn main(args: [str]) {
37+
check vec::is_not_empty(args);
38+
39+
let program : str = vec::head(args);
40+
2341
let opts = [
2442
optopt(\"o\"),
2543
optflag(\"h\"),
2644
optflag(\"help\")
2745
];
28-
let match = alt getopts(vec::shift(args), opts) {
29-
ok(m) { m }
30-
err(f) { fail fail_str(f) }
46+
let match = alt getopts(vec::tail(args), opts) {
47+
result::ok(m) { m }
48+
result::err(f) { fail fail_str(f) }
3149
};
3250
if opt_present(match, \"h\") || opt_present(match, \"help\") {
33-
print_usage();
51+
print_usage(program);
3452
ret;
3553
}
3654
let output = opt_maybe_str(match, \"o\");
37-
let input = if !vec::is_empty(match.free) {
55+
let input = if vec::is_not_empty(match.free) {
3856
match.free[0]
3957
} else {
40-
print_usage();
58+
print_usage(program);
4159
ret;
42-
}
60+
};
4361
do_work(input, output);
4462
}
4563

0 commit comments

Comments
 (0)
Please sign in to comment.