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