octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #62420] inputParser fails due to interpreter c


From: Markus Mützel
Subject: [Octave-bug-tracker] [bug #62420] inputParser fails due to interpreter changes in 7.1.0
Date: Fri, 6 May 2022 12:30:11 -0400 (EDT)

Follow-up Comment #5, bug #62420 (project octave):

With respect to compatibility with Matlab:

>> a = validateattributes('s', {'char'}, {'nonempty'})
Error using validateattributes
Too many output arguments.
 
>> a = validateattributes(1, {'char'}, {'nonempty'})
Error using validateattributes
Too many output arguments.
 
>> validateattributes(1, {'char'}, {'nonempty'})
Expected input to be one of these types:

char

Instead its type was double.
 
>> validateattributes('s', {'char'}, {'nonempty'})


So, IIUC, it is correct that `validateattributes` doesn't return anything in
Octave either.

The change probably needs to happen in the `inputParser` class.

I never used the `inputParser` before. So, I'm a bit at a loss. A minimal
example that reproduces the error would *really* help.
After trying around a bit, I ended up with this that shows that error. (But
I'm not sure if it is supposed to work like this at all.):

p = inputParser ();
p.addRequired ('rootPath', @(s) validateattributes (s, {'char'},
{'nonempty'}));

p.parse ('/usr/bin');


Instead of using a `try`-`catch`-block, would checking `nargout(val)>0` also
work?
Would it be enough for the `inputParser` to directly let the "validation
function" throw its error? Or would it be better to amend the error with
additional information from the `inputParser`?

Something along these lines maybe?

diff -r 1ce1c4552d5b scripts/miscellaneous/inputParser.m
--- a/scripts/miscellaneous/inputParser.m       Thu May 05 20:47:09 2022 +0200
+++ b/scripts/miscellaneous/inputParser.m       Fri May 06 18:26:21 2022 +0200
@@ -543,9 +543,21 @@
 
     function validate_arg (this, name, val, in)
 
-      if (! val (in))
-        this.error (sprintf ("failed validation of %s with %s",
-                             toupper (name), func2str (val)));
+      if (nargout (val) > 0)
+        ok = val (in);
+        err = sprintf ('Checked with "%s"', func2str (val));
+      else
+        try
+          val (in);
+          ok = true;
+        catch exception
+          ok = false;
+          err = exception.message;
+        end_try_catch
+      endif
+      if (! ok)
+        this.error (sprintf ("input %s is invalid. %s",
+                             toupper (name), err));
       endif
       this.Results.(name) = in;
 




    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?62420>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]