Skip to content

Commit

Permalink
Fix sample program to compile in modern rust
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpherrinm committed Feb 7, 2013
1 parent eb28ce0 commit 64fedfb
Showing 1 changed file with 44 additions and 42 deletions.
86 changes: 44 additions & 42 deletions src/libstd/getopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,50 @@
* The following example shows simple command line parsing for an application
* that requires an input file to be specified, accepts an optional output
* file name following -o, and accepts both -h and --help as optional flags.
*
* use std;
* import std::getopts::{optopt,optflag,getopts,opt_present,opt_maybe_str,
* fail_str};
*
* fn do_work(in: str, out: Option<str>) {
* // ...
* }
*
* fn print_usage(program: str) {
* io::println("Usage: " + program + " [options]");
* io::println("-o\t\tOutput");
* io::println("-h --help\tUsage");
* }
*
* fn main(args: ~[str]) {
* check !args.is_empty()
*
* let program : str = vec::head(args);
*
* let opts = ~[
* optopt("o"),
* optflag("h"),
* optflag("help")
* ];
* let matches = match getopts(vec::tail(args), opts) {
* result::ok(m) { m }
* result::err(f) { die!(fail_str(f)) }
* };
* if opt_present(matches, "h") || opt_present(matches, "help") {
* print_usage(program);
* return;
* }
* let output = opt_maybe_str(matches, "o");
* let input = if !matches.free.is_empty() {
* matches.free[0]
* } else {
* print_usage(program);
* return;
* };
* do_work(input, output);
* }
* extern mod std;
* use std::getopts::*;
*
* fn do_work(in: &str, out: Option<~str>) {
* io::println(in);
* io::println(match out {
* Some(move x) => x,
* None => ~"No Output"
* });
* }
*
* fn print_usage(program: &str, _opts: &[std::getopts::Opt]) {
* io::println(fmt!("Usage: %s [options]", program));
* io::println("-o\t\tOutput");
* io::println("-h --help\tUsage");
* }
*
* fn main() {
* let args = os::args();
*
* let program = copy args[0];
*
* let opts = ~[
* optopt("o"),
* optflag("h"),
* optflag("help")
* ];
* let matches = match getopts(vec::tail(args), opts) {
* result::Ok(m) => { m }
* result::Err(f) => { fail fail_str(f) }
* };
* if opt_present(&matches, "h") || opt_present(&matches, "help") {
* print_usage(program, opts);
* return;
* }
* let output = opt_maybe_str(&matches, "o");
* let input: &str = if !matches.free.is_empty() {
* matches.free[0]
* } else {
* print_usage(program, opts);
* return;
* };
* do_work(input, output);
* }
*/
#[forbid(deprecated_mode)];

Expand Down

4 comments on commit 64fedfb

@bors
Copy link
Contributor

@bors bors commented on 64fedfb Feb 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at mcpherrinm@64fedfb

@bors
Copy link
Contributor

@bors bors commented on 64fedfb Feb 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging mcpherrinm/rust/master = 64fedfb into auto

@bors
Copy link
Contributor

@bors bors commented on 64fedfb Feb 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mcpherrinm/rust/master = 64fedfb merged ok, testing candidate = 770677cb

@bors
Copy link
Contributor

@bors bors commented on 64fedfb Feb 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.