changeset 10247:5208f7d4f654 octave-forge

inputparser: deal correctly with unmatched parameters
author carandraug
date Mon, 14 May 2012 08:03:43 +0000
parents ef97c9c3b853
children 48159ee9f688
files main/general/inst/@inputParser/subsref.m
diffstat 1 files changed, 8 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/main/general/inst/@inputParser/subsref.m	Mon May 14 07:35:40 2012 +0000
+++ b/main/general/inst/@inputParser/subsref.m	Mon May 14 08:03:43 2012 +0000
@@ -89,7 +89,7 @@
     [name, inPar.copy] = shift (inPar.copy);
     [value, args]      = shift (args);
     if ( !feval (inPar.Required.(name).validator, value) )
-      error("%sinvalid value for parameter '%s'", inPar.FunctionName, name);
+      error("%sargument '%s' failed validation %s", inPar.FunctionName, name, func2str (inPar.Required.(name).validator));
     endif
     inPar.Results.(name) = value;
   endfor
@@ -163,16 +163,16 @@
       value  = true;
       method = "Switch";
     else
-      ## then it must be a ParamValue, shift its value
+      ## then it must be a ParamValue (even if unmatched), shift its value
       if (numel (args) < 1)
-        error ("%sno value found for Parameter '%s'", inPar.FunctionName, key);
+        error ("%sparameter '%s' does not have a value", inPar.FunctionName, key);
       endif
       [value, args] = shift (args);
       method = "ParamValue";
     endif
 
-    ## index == 0 means no match so either return error or move them into 'Unmatched'
-    if ( index != 0 )
+    ## empty index means no match so either return error or move them into 'Unmatched'
+    if (!isempty (index))
       [name, inPar.copy] = shift (inPar.copy, index);
       if ( !feval (inPar.(method).(name).validator, value))
         error("%sinvalid value for parameter '%s'", inPar.FunctionName, key);
@@ -180,11 +180,11 @@
       ## we use the name shifted from 'copy' instead of the key from 'args' in case
       ## the key is in the wrong case
       inPar.Results.(name) = value;
-    elseif ( index == 0 && inPar.KeepUnmatched )
+    elseif (isempty (index) && inPar.KeepUnmatched )
       inPar.Unmatched.(key) = value;
       i = i - 1; # this time didn't count, go back one
-    elseif ( index == 0 && !inPar.KeepUnmatched )
-      error("%sfound unmatched parameter '%s'", inPar.FunctionName, name);
+    else
+      error ("%sargument '%s' did not match any valid parameter of the parser", inPar.FunctionName, key);
     endif
   endfor