octave-maintainers
[Top][All Lists]
Advanced

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

Re: FYI: parseparams


From: David Grundberg
Subject: Re: FYI: parseparams
Date: Mon, 15 Feb 2010 14:08:54 +0100
User-agent: Thunderbird 2.0.0.23 (X11/20090812)

Jaroslav Hajek wrote:
hi all,

I extended the parseparams function to allow parsing options given as
name-value pairs directly, like this:

function z = my_func (x, y, varargin)
  [~, op, inv_order] = parseparams (varargin, "operation", @plus,
"invertorder", false);
  if (inv_order)
    z = op (y, x);
  else
    z = op (x, y);
  endif
endfunction

this gives:

octave:1> my_func (1, 2)
ans =  3
octave:2> my_func (1, 2, "operation", @minus)
ans = -1
octave:3> my_func (1, 2, "invertOrder", true, "operation", @minus)
ans =  1
octave:4> my_func (1, 2, "invertOrder", true, "Operator", @minus)
error: my_func: unrecognized option: Operator
error: called from:
error:   /home/hajek/devel/octave/main/scripts/miscellaneous/parseparams.m
at line 105, column 3
error:   /home/hajek/devel/octave/main/scripts/miscellaneous/parseparams.m
at line 93, column 9
error:   /home/hajek/devel/octave/main/my_func.m at line 2, column 23
octave:4> my_func (1, 2, "invertOrder", true, "operation")
error: my_func: options must be given as name-value pairs
error: called from:
error:   /home/hajek/devel/octave/main/scripts/miscellaneous/parseparams.m
at line 105, column 3
error:   /home/hajek/devel/octave/main/scripts/miscellaneous/parseparams.m
at line 89, column 9
error:   /home/hajek/devel/octave/main/my_func.m at line 2, column 23

it would be nice if Octave allowed omitting the first two lines from
the trace, but I don't think this is currently possible.
This should make writing functions that take such options easier. Any comments?

http://hg.savannah.gnu.org/hgweb/octave/rev/0d928dd9eeb8

regards


Looks nice. I rolled my own parsing, but this is a good util to have in main Octave.

David


reply via email to

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