Mercurial > octave-nkf
comparison scripts/general/inputParser.m @ 20610:a61f0d6beb71
inputParser: do not perform validation of default values (bug #45837)
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Thu, 08 Oct 2015 20:15:19 +0100 |
parents | 784ed55fdde8 |
children |
comparison
equal
deleted
inserted
replaced
20609:780431fc4137 | 20610:a61f0d6beb71 |
---|---|
301 || numel (fieldnames (this.Switch))) | 301 || numel (fieldnames (this.Switch))) |
302 error (["inputParser.Optional: can't have Optional arguments " ... | 302 error (["inputParser.Optional: can't have Optional arguments " ... |
303 "after ParamValue or Switch"]); | 303 "after ParamValue or Switch"]); |
304 endif | 304 endif |
305 this.validate_name ("Optional", name); | 305 this.validate_name ("Optional", name); |
306 this.validate_default ("Optional", name, def, val); | |
307 this.Optional{end+1} = struct ("name", name, "def", def, "val", val); | 306 this.Optional{end+1} = struct ("name", name, "def", def, "val", val); |
308 endfunction | 307 endfunction |
309 | 308 |
310 function addParamValue (this, name, def, val = inputParser.def_val) | 309 function addParamValue (this, name, def, val = inputParser.def_val) |
311 if (nargin < 3 || nargin > 4) | 310 if (nargin < 3 || nargin > 4) |
312 print_usage (); | 311 print_usage (); |
313 endif | 312 endif |
314 this.validate_name ("ParamValue", name); | 313 this.validate_name ("ParamValue", name); |
315 this.validate_default ("ParamValue", name, def, val); | |
316 this.ParamValue.(name).def = def; | 314 this.ParamValue.(name).def = def; |
317 this.ParamValue.(name).val = val; | 315 this.ParamValue.(name).val = val; |
318 endfunction | 316 endfunction |
319 | 317 |
320 function addSwitch (this, name) | 318 function addSwitch (this, name) |
433 type, name); | 431 type, name); |
434 endif | 432 endif |
435 this.Parameters{end+1} = name; | 433 this.Parameters{end+1} = name; |
436 endfunction | 434 endfunction |
437 | 435 |
438 function validate_default (this, type, name, def, val) | |
439 if (! feval (val, def)) | |
440 error ("inputParser.add%s: failed validation for '%s' default value", | |
441 type, name); | |
442 endif | |
443 endfunction | |
444 | |
445 function validate_arg (this, name, val, in) | 436 function validate_arg (this, name, val, in) |
446 if (! val (in)) | 437 if (! val (in)) |
447 this.error (sprintf ("failed validation of %s", toupper (name))); | 438 this.error (sprintf ("failed validation of %s", toupper (name))); |
448 endif | 439 endif |
449 this.Results.(name) = in; | 440 this.Results.(name) = in; |
580 %! parse (p2, "file", "foo", 80, "line", "circle", "verbose"); | 571 %! parse (p2, "file", "foo", 80, "line", "circle", "verbose"); |
581 %! r = p2.Results; | 572 %! r = p2.Results; |
582 %! assert ({r.req1, r.op1, r.op2, r.verbose, r.line}, | 573 %! assert ({r.req1, r.op1, r.op2, r.verbose, r.line}, |
583 %! {"file", "foo", 80, true, "circle"}); | 574 %! {"file", "foo", 80, true, "circle"}); |
584 | 575 |
576 ## We must not perform validation of default values (bug #45837) | |
577 %!test | |
578 %! p = inputParser; | |
579 %! p.addParamValue ("Dir", [], @ischar); | |
580 %! p.parse (); | |
581 %! assert (p.Results.Dir, []) | |
582 | |
583 %!test | |
584 %! p = inputParser; | |
585 %! p.addParamValue ("positive", -1, @(x) x > 5); | |
586 %! p.parse (); | |
587 %! assert (p.Results.positive, -1) | |
588 | |
585 ## FIXME: This somehow works in Matlab | 589 ## FIXME: This somehow works in Matlab |
586 #%!test | 590 #%!test |
587 #%! p = inputParser; | 591 #%! p = inputParser; |
588 #%! p.addOptional ("op1", "val"); | 592 #%! p.addOptional ("op1", "val"); |
589 #%! p.addParamValue ("line", "tree"); | 593 #%! p.addParamValue ("line", "tree"); |