about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBrian Anderson <andersrb@gmail.com>2011-05-21 21:48:43 -0400
committerBrian Anderson <andersrb@gmail.com>2011-05-22 02:10:09 -0400
commita0f855e48812d8ce5c0c2d4e7496aa0e4643bdb3 (patch)
tree59ce568617c1367e49db134b4c8df79008af535a /src
parent079711d5f62bc440a83066fda87069b611a8e878 (diff)
downloadrust-a0f855e48812d8ce5c0c2d4e7496aa0e4643bdb3.tar.gz
rust-a0f855e48812d8ce5c0c2d4e7496aa0e4643bdb3.zip
stdlib: Report an error when getopts is given an argument to a flag option
Diffstat (limited to 'src')
-rw-r--r--src/lib/getopts.rs7
-rw-r--r--src/test/run-pass/lib-getopts.rs13
2 files changed, 16 insertions, 4 deletions
diff --git a/src/lib/getopts.rs b/src/lib/getopts.rs
index 7318f273d0f..52723575460 100644
--- a/src/lib/getopts.rs
+++ b/src/lib/getopts.rs
@@ -98,6 +98,7 @@ tag fail_ {
     unrecognized_option(str);
     option_missing(str);
     option_duplicated(str);
+    unexpected_argument(str);
 }
 
 fn fail_str(fail_ f) -> str {
@@ -114,6 +115,9 @@ fn fail_str(fail_ f) -> str {
         case (option_duplicated(?nm)) {
             ret "option '" + nm + "' given more than once.";
         }
+        case (unexpected_argument(?nm)) {
+            ret "Option " + nm + " does not take an argument.";
+        }
     }
 }
 
@@ -173,6 +177,9 @@ fn getopts(vec[str] args, vec[opt] opts) -> result {
                 }
                 alt (opts.(optid).hasarg) {
                     case (no) {
+                        if (!option::is_none[str](i_arg)) {
+                            ret failure(unexpected_argument(name_str(nm)));
+                        }
                         vec::push[optval](vals.(optid), given);
                     }
                     case (maybe) {
diff --git a/src/test/run-pass/lib-getopts.rs b/src/test/run-pass/lib-getopts.rs
index fe2d3b7f1c6..044da1ae7ec 100644
--- a/src/test/run-pass/lib-getopts.rs
+++ b/src/test/run-pass/lib-getopts.rs
@@ -9,6 +9,7 @@ tag fail_type {
   unrecognized_option;
   option_missing;
   option_duplicated;
+  unexpected_argument;
 }
 
 fn check_fail_type(opt::fail_ f, fail_type ft) {
@@ -25,6 +26,9 @@ fn check_fail_type(opt::fail_ f, fail_type ft) {
     case (opt::option_duplicated(_)) {
       assert (ft == option_duplicated);
     }
+    case (opt::unexpected_argument(_)) {
+      assert (ft == unexpected_argument);
+    }
     case (_) { fail; }
   }
 }
@@ -262,7 +266,10 @@ fn test_optflag_long_arg() {
   auto opts = [opt::optflag("test")];
   auto res = opt::getopts(args, opts);
   alt (res) {
-    case (opt::failure(?f)) { log_err opt::fail_str(f); }
+    case (opt::failure(?f)) {
+      log_err opt::fail_str(f);
+      check_fail_type(f, unexpected_argument);
+    }
     case (_) { fail; }
   }
 }
@@ -511,9 +518,7 @@ fn main() {
 
   test_optflag_long();
   test_optflag_long_missing();
-  // FIXME: Currently long flags will silently accept arguments
-  // when it should probably report an error
-  //test_optflag_long_arg();
+  test_optflag_long_arg();
   test_optflag_long_multi();
 
   test_optflag_short();