# HG changeset patch # User Carnë Draug # Date 1491756357 -3600 # Node ID f1bf2590272a3ad28d3c8f4ba98a92d4cf05f3a1 # Parent 7332287221a922ec38aacc1a41bb50d16d9ae7a1 inputParser.m: do not confuse Switch options with positional options (bug #50752) diff -r 7332287221a9 -r f1bf2590272a scripts/general/inputParser.m --- a/scripts/general/inputParser.m Sun Apr 09 07:46:46 2017 +0200 +++ b/scripts/general/inputParser.m Sun Apr 09 17:45:57 2017 +0100 @@ -402,9 +402,12 @@ while (vidx < pnargin && idx < nOpt) opt = this.Optional{++idx}; in = varargin{++vidx}; - if (this.is_argname ("Parameter", in) && vidx < pnargin) - ## This looks like an optional parameter/value pair, not an - ## optional positional parameter. + if ((this.is_argname ("Parameter", in) && vidx < pnargin) + || this.is_argname ("Switch", in)) + ## This looks like an optional parameter/value pair or a + ## switch, not an positional option. This does mean that + ## positional options cannot be strings named like parameter + ## keys. See bug #50752. idx -= 1; vidx -= 1; break @@ -417,8 +420,8 @@ if (! valid_option) ## If it does not match there's two options: ## 1) input is actually wrong and we should error; - ## 2) it's a Parameter or Switch name and we should use the - ## the default for the rest. + ## 2) it's a Parameter or Switch name and we should use + ## the default for the rest; ## 3) it's a struct with the Parameter pairs. if (ischar (in) || (this.StructExpand && isstruct (in) && isscalar (in))) @@ -734,13 +737,32 @@ %! p.parse ("foo", "qux"); %! assert (p.Results, struct ("foo", "qux")) -## FIXME: This somehow works in Matlab +## This behaviour means that a positional option can never be a string +## that is the name of a parameter key. This is required for Matlab +## compatibility. %!test <50752> %! p = inputParser; %! p.addOptional ("op1", "val"); %! p.addParameter ("line", "tree"); %! p.parse ("line", "circle"); -%! assert (p.Results, struct ("op1", "val", "line", "circle")); +%! assert (p.Results, struct ("op1", "val", "line", "circle")) +%! +%! p = inputParser (); +%! p.addOptional ("op1", "val1") +%! p.addOptional ("op2", "val2") +%! p.addParameter ("line", "tree") +%! p.parse ("line", "circle") +%! assert (p.Results.op1, "val1") +%! assert (p.Results.op2, "val2") +%! assert (p.Results.line, "circle") + +%!test <50752> +%! p = inputParser; +%! p.addOptional ("op1", "val1"); +%! p.addSwitch ("line"); +%! p.parse ("line"); +%! assert (p.Results.op1, "val1") +%! assert (p.Results.line, true) %!test %! p = inputParser;