|
29 | 29 | * The following example shows simple command line parsing for an application
|
30 | 30 | * that requires an input file to be specified, accepts an optional output
|
31 | 31 | * file name following -o, and accepts both -h and --help as optional flags.
|
| 32 | + * extern mod std; |
| 33 | + * use std::getopts::*; |
32 | 34 | *
|
33 |
| - * use std; |
34 |
| - * import std::getopts::{optopt,optflag,getopts,opt_present,opt_maybe_str, |
35 |
| - * fail_str}; |
| 35 | + * fn do_work(in: &str, out: Option<~str>) { |
| 36 | + * io::println(in); |
| 37 | + * io::println(match out { |
| 38 | + * Some(move x) => x, |
| 39 | + * None => ~"No Output" |
| 40 | + * }); |
| 41 | + * } |
36 | 42 | *
|
37 |
| - * fn do_work(in: str, out: Option<str>) { |
38 |
| - * // ... |
39 |
| - * } |
| 43 | + * fn print_usage(program: &str, _opts: &[std::getopts::Opt]) { |
| 44 | + * io::println(fmt!("Usage: %s [options]", program)); |
| 45 | + * io::println("-o\t\tOutput"); |
| 46 | + * io::println("-h --help\tUsage"); |
| 47 | + * } |
40 | 48 | *
|
41 |
| - * fn print_usage(program: str) { |
42 |
| - * io::println("Usage: " + program + " [options]"); |
43 |
| - * io::println("-o\t\tOutput"); |
44 |
| - * io::println("-h --help\tUsage"); |
45 |
| - * } |
| 49 | + * fn main() { |
| 50 | + * let args = os::args(); |
46 | 51 | *
|
47 |
| - * fn main(args: ~[str]) { |
48 |
| - * check !args.is_empty() |
| 52 | + * let program = copy args[0]; |
49 | 53 | *
|
50 |
| - * let program : str = vec::head(args); |
51 |
| - * |
52 |
| - * let opts = ~[ |
53 |
| - * optopt("o"), |
54 |
| - * optflag("h"), |
55 |
| - * optflag("help") |
56 |
| - * ]; |
57 |
| - * let matches = match getopts(vec::tail(args), opts) { |
58 |
| - * result::ok(m) { m } |
59 |
| - * result::err(f) { die!(fail_str(f)) } |
60 |
| - * }; |
61 |
| - * if opt_present(matches, "h") || opt_present(matches, "help") { |
62 |
| - * print_usage(program); |
63 |
| - * return; |
64 |
| - * } |
65 |
| - * let output = opt_maybe_str(matches, "o"); |
66 |
| - * let input = if !matches.free.is_empty() { |
67 |
| - * matches.free[0] |
68 |
| - * } else { |
69 |
| - * print_usage(program); |
70 |
| - * return; |
71 |
| - * }; |
72 |
| - * do_work(input, output); |
73 |
| - * } |
| 54 | + * let opts = ~[ |
| 55 | + * optopt("o"), |
| 56 | + * optflag("h"), |
| 57 | + * optflag("help") |
| 58 | + * ]; |
| 59 | + * let matches = match getopts(vec::tail(args), opts) { |
| 60 | + * result::Ok(m) => { m } |
| 61 | + * result::Err(f) => { fail fail_str(f) } |
| 62 | + * }; |
| 63 | + * if opt_present(&matches, "h") || opt_present(&matches, "help") { |
| 64 | + * print_usage(program, opts); |
| 65 | + * return; |
| 66 | + * } |
| 67 | + * let output = opt_maybe_str(&matches, "o"); |
| 68 | + * let input: &str = if !matches.free.is_empty() { |
| 69 | + * matches.free[0] |
| 70 | + * } else { |
| 71 | + * print_usage(program, opts); |
| 72 | + * return; |
| 73 | + * }; |
| 74 | + * do_work(input, output); |
| 75 | + * } |
74 | 76 | */
|
75 | 77 |
|
76 | 78 | use core::cmp::Eq;
|
|
0 commit comments