summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorBrian Anderson <andersrb@gmail.com>2012-05-01 10:30:39 -0700
committerBrian Anderson <andersrb@gmail.com>2012-05-01 10:30:39 -0700
commit36f7ace839d378c5da68c08ef3a9cf3c8f7b24c5 (patch)
treea4d3a32d2c4056ea1827263ee7b0fab1ba697c7a /src/libstd
parent46425af8a5472ea91a0e36ba982a72f30681c275 (diff)
parent1a19309d9121f68568c83b58bcc316112e63a966 (diff)
downloadrust-36f7ace839d378c5da68c08ef3a9cf3c8f7b24c5.tar.gz
rust-36f7ace839d378c5da68c08ef3a9cf3c8f7b24c5.zip
Merge pull request #2322 from bkircher/fix-getopts-docs
Fix getopts docs
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/getopts.rs36
1 files changed, 27 insertions, 9 deletions
diff --git a/src/libstd/getopts.rs b/src/libstd/getopts.rs
index beec5638b84..975e7089481 100644
--- a/src/libstd/getopts.rs
+++ b/src/libstd/getopts.rs
@@ -4,12 +4,12 @@ Simple getopt alternative.
 Construct a vector of options, either by using reqopt, optopt, and optflag or
 by building them from components yourself, and pass them to getopts, along
 with a vector of actual arguments (not including argv[0]). You'll either get a
-failure code back, or a match.  You'll have to verify whether the amount of
+failure code back, or a match. You'll have to verify whether the amount of
 'free' arguments in the match is what you expect. Use opt_* accessors to get
 argument values out of the match object.
 
 Single-character options are expected to appear on the command line with a
-single preceeding dash; multiple-character options are expected to be
+single preceding dash; multiple-character options are expected to be
 proceeded by two dashes. Options that expect an argument accept their argument
 following either a space or an equals sign.
 
@@ -19,27 +19,45 @@ 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 vec::is_not_empty(args);
+
+        let program : str = vec::head(args);
+
         let opts = [
             optopt(\"o\"),
             optflag(\"h\"),
             optflag(\"help\")
         ];
-        let match = alt getopts(vec::shift(args), opts) {
-          ok(m) { m }
-          err(f) { fail fail_str(f) }
+        let match = alt getopts(vec::tail(args), opts) {
+            result::ok(m) { m }
+            result::err(f) { fail fail_str(f) }
         };
         if opt_present(match, \"h\") || opt_present(match, \"help\") {
-            print_usage();
+            print_usage(program);
             ret;
         }
         let output = opt_maybe_str(match, \"o\");
-        let input = if !vec::is_empty(match.free) {
+        let input = if vec::is_not_empty(match.free) {
             match.free[0]
         } else {
-            print_usage();
+            print_usage(program);
             ret;
-        }
+        };
         do_work(input, output);
     }