changeset 21666:09517e0a8aa0

inputParser.m: handle errors in Optional parameters (maybe is a param key).
author Carnë Draug <carandraug@octave.org>
date Tue, 03 May 2016 03:09:49 +0100
parents 7b48be706e28
children 84092ccb45e2
files scripts/general/inputParser.m
diffstat 1 files changed, 17 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/inputParser.m	Mon May 02 18:40:24 2016 -0400
+++ b/scripts/general/inputParser.m	Tue May 03 03:09:49 2016 +0100
@@ -354,7 +354,12 @@
       while (vidx < pnargin && idx < nOpt)
         opt = this.Optional{++idx};
         in  = varargin{++vidx};
-        if (! opt.val (in))
+        try
+          valid_option = opt.val (in);
+        catch
+          valid_option = false;
+        end_try_catch
+        if (! valid_option)
           ## If it does not match there's two options:
           ##    1) input is actually wrong and we should error;
           ##    2) it's a ParamValue or Switch name and we should use the
@@ -589,6 +594,17 @@
 %! p.parse ();
 %! assert (p.Results.positive, -1);
 
+## Throw an error on validation of optional argument to check that it
+## is caught without preventing continuation into param/value pairs.
+%!test
+%! p = inputParser ();
+%! p.addOptional ("err", "foo", @error);
+%! p.addParamValue ("not_err", "bar", @ischar);
+%! p.parse ("not_err", "qux");
+%! assert (p.Results.err, "foo")
+%! assert (p.Results.not_err, "qux")
+
+
 %!function r = foobar (varargin)
 %!  p = inputParser ();
 %!  p.addParamValue ("foo", "bar", @ischar);