bugGNU Octave - Bugs: bug #62639, inputParser fails to recognize...

 
 

bug #62639: inputParser fails to recognize Optional parameter with argument

Submitter:  None
Submitted:  Thu 16 Jun 2022 11:45:16 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  2 Item Group:  Undocumented Matlab
Status:  Wont Fix Assigned to:  None
Originator Name:  Nikita Beliy Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * dev
Operating System:  * Any Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Sat 15 Apr 2023 02:34:21 PM UTC, comment #13: 

The original issue of the bug report has been fixed.  The second issue is undocumented Matlab which Octave does not generally attempt to follow because it may change at any time and is most likely an artifact of the particular implementation for a particular release.

In this case, it doesn't even seem to make particular sense why the interpreter should ever treat an Optional argument as a Parameter/Value pair when the programmer specifically asked for it to be treated as a positional argument.

Closing the report as "Won't Fix".

Rik <rik5>
Group administrator
Wed 06 Jul 2022 06:43:51 PM UTC, comment #12: 

I think this is an edge case which is undocumented Matlab behavior (likely an artifact of the implementation) and therefore Octave shouldn't try too hard to try and duplicate this.  For reference, Matlab documentation for addOptional (https://www.mathworks.com/help/matlab/ref/inputparser.addoptional.html) does not mention this fallback behavior of interpreting a positional argument as a Parameter if the name matches.

And there is an easy workaround here.  If the programmer wants a positional argument then use addOptional.  If they want a name/value pair then use addParameter.  It seems odd to define an input scheme one way (positional) and then expect it to work the other way around as well.

Rik <rik5>
Group administrator
Wed 06 Jul 2022 10:51:08 AM UTC, comment #11: 

OP here.

In terms of inputParser, 'Optional' are positional parameters that are not mandatory to pass, while the 'Parameter' are named parameters, that you should first pass the name of parameter and then the value.

In your first example

p.addParameter('opt',{});
p.parse('opt', 'xxx')

'opt' is the name, and 'xxx' is the argument.

In second example:

p.addOptional('opt',{});
p.parse('opt', 'xxx')

'opt' is the argument, and following one 'xxx' is not defined.

In Matlab, however if first argument has name of one of the options, then it tries to interpret it as parameter and not option.

I hope it make the issue clearer.

From user point of view, change Options to Parameters require little effort.
Not sure if replicate this behavior in Octave is necessary.

Anonymous
Tue 05 Jul 2022 06:19:02 PM UTC, comment #10: 

I think this is a new problem, but let's continue with this bug report.

The code doesn't seem to recognize that the first argument is an optional parameter and that it should be proceed down that path of setting the optional parameter.  For reference, this code works


p = inputParser;
p.addParameter('opt',{});
p.parse('opt', 'xxx')


while this code fails


p = inputParser;
p.addOptional('opt',{});
p.parse('opt', 'xxx')
error: argument 'XXX' is not a valid parameter


The code for this starts at line 381 in scripts/miscellaneous/inputParser.m at "function parse".  I took a brief look, but I found the code difficult to understand.  There is a search for Optional parameters according to the comment at line 418.


      ## Search for a list of Optional arguments




Rik <rik5>
Group administrator
Tue 05 Jul 2022 03:34:29 PM UTC, comment #9: 

typo: "past" should have been "passed"

Markus Mützel <mmuetzel>
Group administrator
Tue 05 Jul 2022 03:33:52 PM UTC, comment #8: 

I'm not sure how that is supposed to work. I was assuming that in the command `p.parse('opt', 'xxx')` two values are past to the inputParser object. But only one (optional) parameter was defined before. That's the cause of the error.

Should that be working differently?

Markus Mützel <mmuetzel>
Group administrator
Tue 05 Jul 2022 03:13:58 PM UTC, comment #7: 

comment #6:

> It still seems there is a problem.  Using the original test code with current dev branch of Octave I get


OP here.

The original test code contains an unrelated error: an optional
argument is treated as a parameter.

In matlab optional values can be also treated as parameters
in p.parse, but it is undocumented (at least on matlab central).

I discovered only after reporting the bug.



Anonymous
Tue 05 Jul 2022 03:04:21 PM UTC, comment #6: 

It still seems there is a problem.  Using the original test code with current dev branch of Octave I get


octave:10> p = inputParser;
octave:11> p.addOptional('opt',{});
octave:12> p.parse('opt', 'xxx')
error: argument 'XXX' is not a valid parameter
error: called from
    error at line 615 column 7
    parse at line 500 column 13


The original error, "error: value on right hand side of assignment is undefined", which was generated by the second line of code has been fixed.

The new error is generated by the third line of code.  Matlab executes this code just fine.

Rik <rik5>
Group administrator
Tue 21 Jun 2022 01:45:56 PM UTC, comment #5: 

I pushed a fix to the stable branch here:
https://hg.savannah.gnu.org/hgweb/octave/rev/c415b218307f

I forgot to include some BISTs in that changeset. So, I followed up with some tests for this here:
https://hg.savannah.gnu.org/hgweb/octave/rev/7797481038fc

Marking as ready for test.

Markus Mützel <mmuetzel>
Group administrator
Tue 21 Jun 2022 01:23:19 PM UTC, comment #4: 

Not a regression. But might still be worth fixing on stable.

Markus Mützel <mmuetzel>
Group administrator
Tue 21 Jun 2022 01:18:31 PM UTC, comment #3: 

IIUC, this is an issue whenever the default value for an optional parameter is a cell. (Not only for empty cells.)

An alternative (maybe less invasive) fix might be:

diff -r 05729b0e39e4 scripts/miscellaneous/inputParser.m
--- a/scripts/miscellaneous/inputParser.m        Mon Jun 20 17:27:39 2022 -0400
+++ b/scripts/miscellaneous/inputParser.m        Tue Jun 21 15:17:05 2022 +0200
@@ -275,6 +275,9 @@
                 "after Parameter or Switch"]);
       endif
       this.validate_name ("Optional", name);
+      if (iscell (def))
+        def = {def};
+      endif
       this.Optional{end+1} = struct ("name", name, "def", def, "val", val);
     endfunction


Markus Mützel <mmuetzel>
Group administrator
Tue 21 Jun 2022 12:04:00 PM UTC, comment #2: 

OP here.

An easy solution will be to use instead of cellarray of structures, the structarray:

In inputParser.m:276

Optional = struct("name", {}, "def", {}, "val", {});


Filling (line 328):

this.Optional{end+1}.name = name;
this.Optional{end}.def = def;
this.Optional{end}.val = val;


Accessing (line 401):
+verbatum+
opt = this.Optional(++idx);
-verbatum-
Essentually replacing {} by ().

Anonymous
Thu 16 Jun 2022 03:48:47 PM UTC, comment #1: 

Confirmed.  This is still an issue with the current development version so changing release to "dev".

John W. Eaton <jwe>
Group administrator
Thu 16 Jun 2022 11:45:16 AM UTC, original submission:  

When creating argument parser with an empty optional argument:

octave:

> p = inputParser;
> p.addOptional('opt',{});
> p.parse('opt', 'xxx')

fails with error:

 error: value on right hand side of assignment is undefined
 error: called from
   parse at line 441 column 35


In Matlab (R2017b) works as expected:

matlab:

>> p = inputParser;
>> p.addOptional('opt',{});
>> p.parse('opt', 'xxx');
>> p.Results
ans =

  struct with fields:

    opt: 'xxx'


The problem comes from inputParser.m:324:

this.Optional{end+1} = struct ("name", name, "def", def, "val", val);


The creation of structure with an empty cell as value creates an empty structure (both in matlab and octave):

octave:

> sp = struc(p);
> sp.Optional
ans =
{
  [1,1] =

    0x0 struct array containing the fields:

      name
      def
      val

}


matlab:

>> sp = struct(p);
>> sp.Optional

ans =

  struct with fields:

         name: 'opt'
      default: {}
    validator: []


P.S. Can't test later versions of octave, the 5.2.0-1 is the latest one for Ubuntu 20.04

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by nrjank (Updated the item)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 14 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-04-15 rik5 StatusConfirmed Wont Fix
        Open/ClosedOpen Closed
    2022-07-06 rik5 Priority5 - Normal 2
        Item GroupUnexpected Error or Warning Undocumented Matlab
        Release7.1.90 dev
    2022-07-05 rik5 SummaryinputParser fails to add Optional argument with empty cell default value inputParser fails to recognize Optional parameter with argument
    2022-07-05 rik5 StatusReady For Test Confirmed
    2022-06-21 mmuetzel StatusConfirmed Ready For Test
    2022-06-21 mmuetzel Releasedev 7.1.90
        Operating SystemGNU/Linux Any
    2022-06-16 nrjank SummaryimputParser fails to add Optional argument with empty cell default value inputParser fails to add Optional argument with empty cell default value
    2022-06-16 jwe Item GroupMatlab Compatibility Unexpected Error or Warning
        StatusNone Confirmed
        Release5.2.0 dev

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code