annotate scripts/miscellaneous/inputParser.m @ 28917:72d57dbcc305

maint: Add semicolon after break and return keywords. * __unimplemented__.m, logm.m, inputParser.m, install.m, camlookat.m, ginput.m, __gnuplot_draw_axes__.m, subplot.m, __splinefit__.m, ismember.m, bicg.m, bicgstab.m, cgs.m, gmres.m, pcg.m, tfqmr.m, treelayout.m: Add semicolon after break and return keywords.
author Rik <rik@octave.org>
date Tue, 13 Oct 2020 20:12:56 -0700
parents 0de38a6ef693
children 9e43deb9bfc3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
3 ## Copyright (C) 2011-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27216
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
7 ##
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
8 ## This file is part of Octave.
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22559
diff changeset
13 ## (at your option) any later version.
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
14 ##
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22559
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22559
diff changeset
18 ## GNU General Public License for more details.
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
19 ##
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
25
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
26 classdef inputParser < handle
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
27
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
28 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
29 ## @deftypefn {} {@var{p} =} inputParser ()
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
30 ## Create object @var{p} of the inputParser class.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
31 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
32 ## This class is designed to allow easy parsing of function arguments. The
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
33 ## class supports four types of arguments:
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
34 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
35 ## @enumerate
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
36 ## @item mandatory (see @code{addRequired});
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
37 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
38 ## @item optional (see @code{addOptional});
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
39 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
40 ## @item named (see @code{addParameter});
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
41 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
42 ## @item switch (see @code{addSwitch}).
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
43 ## @end enumerate
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
44 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
45 ## After defining the function API with these methods, the supplied arguments
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
46 ## can be parsed with the @code{parse} method and the parsing results
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
47 ## accessed with the @code{Results} accessor.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
48 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
49 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
50 ## @deftypefn {} {} inputParser.Parameters
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
51 ## Return list of parameter names already defined.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
52 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
53 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
54 ## @deftypefn {} {} inputParser.Results
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
55 ## Return structure with argument names as fieldnames and corresponding values.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
56 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
57 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
58 ## @deftypefn {} {} inputParser.Unmatched
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
59 ## Return structure similar to @code{Results}, but for unmatched parameters.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
60 ## See the @code{KeepUnmatched} property.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
61 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
62 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
63 ## @deftypefn {} {} inputParser.UsingDefaults
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
64 ## Return cell array with the names of arguments that are using default values.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
65 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
66 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
67 ## @deftypefn {} {} inputParser.CaseSensitive = @var{boolean}
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
68 ## Set whether matching of argument names should be case sensitive. Defaults
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
69 ## to false.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
70 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
71 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
72 ## @deftypefn {} {} inputParser.FunctionName = @var{name}
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
73 ## Set function name to be used in error messages; Defaults to empty string.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
74 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
75 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
76 ## @deftypefn {} {} inputParser.KeepUnmatched = @var{boolean}
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
77 ## Set whether an error should be given for non-defined arguments. Defaults to
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
78 ## false. If set to true, the extra arguments can be accessed through
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
79 ## @code{Unmatched} after the @code{parse} method. Note that since
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
80 ## @code{Switch} and @code{Parameter} arguments can be mixed, it is
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
81 ## not possible to know the unmatched type. If argument is found unmatched
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
82 ## it is assumed to be of the @code{Parameter} type and it is expected to
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
83 ## be followed by a value.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
84 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
85 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
86 ## @deftypefn {} {} inputParser.StructExpand = @var{boolean}
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
87 ## Set whether a structure can be passed to the function instead of
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
88 ## parameter/value pairs. Defaults to true.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
89 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
90 ## The following example shows how to use this class:
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
91 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
92 ## @example
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
93 ## function check (varargin)
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
94 ## @c The next two comments need to be indented by one for alignment
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
95 ## p = inputParser (); # create object
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
96 ## p.FunctionName = "check"; # set function name
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
97 ## p.addRequired ("pack", @@ischar); # mandatory argument
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
98 ## p.addOptional ("path", pwd(), @@ischar); # optional argument
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
99 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
100 ## ## create a function handle to anonymous functions for validators
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
101 ## val_mat = @@(x) isvector (x) && all (x <= 1) && all (x >= 0);
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
102 ## p.addOptional ("mat", [0 0], val_mat);
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
103 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
104 ## ## create two arguments of type "Parameter"
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
105 ## val_type = @@(x) any (strcmp (x, @{"linear", "quadratic"@}));
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
106 ## p.addParameter ("type", "linear", val_type);
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
107 ## val_verb = @@(x) any (strcmp (x, @{"low", "medium", "high"@}));
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
108 ## p.addParameter ("tolerance", "low", val_verb);
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
109 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
110 ## ## create a switch type of argument
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
111 ## p.addSwitch ("verbose");
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
112 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
113 ## p.parse (varargin@{:@}); # Run created parser on inputs
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
114 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
115 ## ## the rest of the function can access inputs by using p.Results.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
116 ## ## for example, get the tolerance input with p.Results.tolerance
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
117 ## endfunction
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
118 ## @end example
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
119 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
120 ## @example
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
121 ## @group
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
122 ## check ("mech"); # valid, use defaults for other arguments
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
123 ## check (); # error, one argument is mandatory
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
124 ## check (1); # error, since ! ischar
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
125 ## check ("mech", "~/dev"); # valid, use defaults for other arguments
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
126 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
127 ## check ("mech", "~/dev", [0 1 0 0], "type", "linear"); # valid
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
128 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
129 ## ## following is also valid. Note how the Switch argument type can
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
130 ## ## be mixed into or before the Parameter argument type (but it
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
131 ## ## must still appear after any Optional argument).
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
132 ## check ("mech", "~/dev", [0 1 0 0], "verbose", "tolerance", "high");
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
133 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
134 ## ## following returns an error since not all optional arguments,
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
135 ## ## 'path' and 'mat', were given before the named argument 'type'.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
136 ## check ("mech", "~/dev", "type", "linear");
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
137 ## @end group
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
138 ## @end example
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
139 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
140 ## @emph{Note 1}: A function can have any mixture of the four API types but
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
141 ## they must appear in a specific order. @code{Required} arguments must be
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
142 ## first and can be followed by any @code{Optional} arguments. Only
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
143 ## the @code{Parameter} and @code{Switch} arguments may be mixed
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
144 ## together and they must appear at the end.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
145 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
146 ## @emph{Note 2}: If both @code{Optional} and @code{Parameter} arguments
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
147 ## are mixed in a function API then once a string Optional argument fails to
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
148 ## validate it will be considered the end of the @code{Optional}
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
149 ## arguments. The remaining arguments will be compared against any
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
150 ## @code{Parameter} or @code{Switch} arguments.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
151 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
152 ## @seealso{nargin, validateattributes, validatestring, varargin}
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
153 ## @end deftypefn
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
154
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
155 properties
21578
683a1beee538 maint: Use "FIXME:" for all code blocks needing further attention.
Rik <rik@octave.org>
parents: 21568
diff changeset
156 ## FIXME: set input checking for these properties
21568
3d60ed163b70 maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents: 21546
diff changeset
157 CaseSensitive = false;
3d60ed163b70 maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents: 21546
diff changeset
158 FunctionName = "";
3d60ed163b70 maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents: 21546
diff changeset
159 KeepUnmatched = false;
21937
55f7de37b618 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
160 PartialMatching = false; # FIXME: unimplemented (and default should be true)
21667
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
161 StructExpand = true;
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
162 endproperties
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
163
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
164 properties (SetAccess = protected)
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19597
diff changeset
165 Parameters = cell ();
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19597
diff changeset
166 Results = struct ();
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19597
diff changeset
167 Unmatched = struct ();
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19597
diff changeset
168 UsingDefaults = cell ();
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
169 endproperties
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
170
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
171 properties (Access = protected)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
172 ## Since Required and Optional are ordered, they get a cell array of
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
173 ## structs with the fields "name", "def" (default), and "val" (validator).
23164
99ca557fd34f maint: Use Octave whitespace conventions for scripts/general/inputParser.m.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 22755
diff changeset
174 Required = cell ();
99ca557fd34f maint: Use Octave whitespace conventions for scripts/general/inputParser.m.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 22755
diff changeset
175 Optional = cell ();
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
176 ## Parameter and Switch are unordered so we have a struct whose fieldnames
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
177 ## are the argname, and values are a struct with fields "def" and "val"
23164
99ca557fd34f maint: Use Octave whitespace conventions for scripts/general/inputParser.m.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 22755
diff changeset
178 Parameter = struct ();
99ca557fd34f maint: Use Octave whitespace conventions for scripts/general/inputParser.m.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 22755
diff changeset
179 Switch = struct ();
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
180
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
181 ## List of Parameter and Switch names to ease searches
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
182 ParameterNames = cell ();
23164
99ca557fd34f maint: Use Octave whitespace conventions for scripts/general/inputParser.m.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 22755
diff changeset
183 SwitchNames = cell ();
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
184
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
185 ## When checking for fieldnames in a Case Insensitive way, this variable
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
186 ## holds the correct identifier for the last searched named using the
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
187 ## is_argname method.
21568
3d60ed163b70 maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents: 21546
diff changeset
188 last_name = "";
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
189 endproperties
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
190
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
191 properties (Access = protected, Constant = true)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
192 ## Default validator, always returns scalar true.
28563
5a07c798eb08 avoid function call input or output argument number mismatch
John W. Eaton <jwe@octave.org>
parents: 28378
diff changeset
193 def_val = @(~) true;
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
194 endproperties
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
195
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
196 methods
21668
58f5a6347851 inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents: 21667
diff changeset
197 function set.PartialMatching (this, val)
58f5a6347851 inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents: 21667
diff changeset
198 if (val)
58f5a6347851 inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents: 21667
diff changeset
199 error ("inputParser: PartialMatching is not yet implemented");
58f5a6347851 inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents: 21667
diff changeset
200 endif
58f5a6347851 inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents: 21667
diff changeset
201 endfunction
58f5a6347851 inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents: 21667
diff changeset
202
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
203 function addRequired (this, name, val = inputParser.def_val)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
204
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
205 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
206 ## @deftypefn {} {} addRequired (@var{argname})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
207 ## @deftypefnx {} {} addRequired (@var{argname}, @var{validator})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
208 ## Add new mandatory argument to the object @var{parser} of inputParser class.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
209 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
210 ## This method belongs to the inputParser class and implements an ordered
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
211 ## arguments type of API.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
212 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
213 ## @var{argname} must be a string with the name of the new argument. The order
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
214 ## in which new arguments are added with @code{addrequired}, represents the
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
215 ## expected order of arguments.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
216 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
217 ## @var{validator} is an optional function handle to validate the given values
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
218 ## for the argument with name @var{argname}. Alternatively, a function name
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
219 ## can be used.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
220 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
221 ## See @code{help inputParser} for examples.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
222 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
223 ## @emph{Note}: this can be used together with the other type of arguments but
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
224 ## it must be the first (see @code{@@inputParser}).
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
225 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
226 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
227
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 28563
diff changeset
228 if (nargin < 2)
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
229 print_usage ();
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
230 elseif (numel (this.Optional) || numfields (this.Parameter)
20937
3b2cc6ef0624 use numfields (x) instead of numel (fieldnames (x))
John W. Eaton <jwe@octave.org>
parents: 20852
diff changeset
231 || numfields (this.Switch))
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
232 error (["inputParser.addRequired: can't have a Required argument " ...
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
233 "after Optional, Parameter, or Switch"]);
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
234 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
235 this.validate_name ("Required", name);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
236 this.Required{end+1} = struct ("name", name, "val", val);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
237 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
238
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
239 function addOptional (this, name, def, val = inputParser.def_val)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
240
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
241 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
242 ## @deftypefn {} {} addOptional (@var{argname}, @var{default})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
243 ## @deftypefnx {} {} addOptional (@var{argname}, @var{default}, @var{validator})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
244 ## Add new optional argument to the object @var{parser} of the class
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
245 ## inputParser to implement an ordered arguments type of API
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
246 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
247 ## @var{argname} must be a string with the name of the new argument. The order
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
248 ## in which new arguments are added with @code{addOptional}, represents the
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
249 ## expected order of arguments.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
250 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
251 ## @var{default} will be the value used when the argument is not specified.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
252 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
253 ## @var{validator} is an optional anonymous function to validate the given
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
254 ## values for the argument with name @var{argname}. Alternatively, a
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
255 ## function name can be used.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
256 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
257 ## See @code{help inputParser} for examples.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
258 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
259 ## @emph{Note}: if a string argument does not validate, it will be considered a
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
260 ## ParamValue key. If an optional argument is not given a validator, anything
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
261 ## will be valid, and so any string will be considered will be the value of the
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
262 ## optional argument (in @sc{matlab}, if no validator is given and argument is
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
263 ## a string it will also be considered a ParamValue key).
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
264 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
265 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
266
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 28563
diff changeset
267 if (nargin < 3)
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
268 print_usage ();
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
269 elseif (numfields (this.Parameter) || numfields (this.Switch))
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
270 error (["inputParser.Optional: can't have Optional arguments " ...
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
271 "after Parameter or Switch"]);
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
272 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
273 this.validate_name ("Optional", name);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
274 this.Optional{end+1} = struct ("name", name, "def", def, "val", val);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
275 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
276
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
277 function addParamValue (this, name, def, val = inputParser.def_val)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
278
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
279 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
280 ## @deftypefn {} {} addParamValue (@var{argname}, @var{default})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
281 ## @deftypefnx {} {} addParamValue (@var{argname}, @var{default}, @var{validator})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
282 ## Add new parameter to the object @var{parser} of the class inputParser to
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
283 ## implement a name/value pair type of API.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
284 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
285 ## This is an alias for @code{addParameter} method without the
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
286 ## @qcode{"PartialMatchPriority"} option. See it for the help text.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
287 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
288 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
289
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 28563
diff changeset
290 if (nargin < 3)
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
291 print_usage ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
292 endif
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
293 this.addParameter (name, def, val);
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
294 endfunction
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
295
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
296 function addParameter (this, name, def, varargin)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
297
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
298 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
299 ## @deftypefn {} {} addParameter (@var{argname}, @var{default})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
300 ## @deftypefnx {} {} addParameter (@var{argname}, @var{default}, @var{validator})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
301 ## Add new parameter to the object @var{parser} of the class inputParser to
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
302 ## implement a name/value pair type of API.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
303 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
304 ## @var{argname} must be a string with the name of the new parameter.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
305 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
306 ## @var{default} will be the value used when the parameter is not specified.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
307 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
308 ## @var{validator} is an optional function handle to validate the given values
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
309 ## for the parameter with name @var{argname}. Alternatively, a function name
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
310 ## can be used.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
311 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
312 ## See @code{help inputParser} for examples.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
313 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
314 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
315
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
316 if (nargin < 3 || nargin > 6)
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
317 print_usage ();
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
318 endif
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
319
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
320 n_opt = numel (varargin);
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
321
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
322 if (n_opt == 0 || n_opt == 2)
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
323 val = inputParser.def_val;
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
324 else # n_opt is 1 or 3
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
325 val = varargin{1};
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
326 endif
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
327
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
328 if (n_opt == 0 || n_opt == 1)
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
329 match_priority = 1;
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
330 else # n_opt is 2 or 3
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
331 if (! strcmpi (varargin{end-1}, "PartialMatchPriority"))
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
332 error ("inputParser.addParameter: unrecognized option");
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
333 endif
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
334 match_priority = varargin{end};
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
335 validateattributes (match_priority, {"numeric"}, {"positive", "integer"},
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
336 "inputParser.addParameter",
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
337 "PartialMatchPriority");
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
338 endif
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
339
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
340 this.validate_name ("Parameter", name);
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
341 this.Parameter.(name).def = def;
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
342 this.Parameter.(name).val = val;
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
343 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
344
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
345 function addSwitch (this, name)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
346
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
347 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
348 ## @deftypefn {} {} addSwitch (@var{argname})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
349 ## Add new switch type of argument to the object @var{parser} of inputParser
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
350 ## class.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
351 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
352 ## This method belongs to the inputParser class and implements a switch
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
353 ## arguments type of API.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
354 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
355 ## @var{argname} must be a string with the name of the new argument. Arguments
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
356 ## of this type can be specified at the end, after @code{Required} and
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
357 ## @code{Optional}, and mixed between the @code{Parameter}. They default to
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
358 ## false. If one of the arguments supplied is a string like @var{argname},
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
359 ## then after parsing the value of @var{parse}.Results.@var{argname} will be
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
360 ## true.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
361 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
362 ## See @code{help inputParser} for examples.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
363 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
364 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
365
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
366 if (nargin != 2)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
367 print_usage ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
368 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
369 this.validate_name ("Switch", name);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
370 this.Switch.(name).def = false;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
371 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
372
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
373 function parse (this, varargin)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
374
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
375 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
376 ## @deftypefn {} {} parse (@var{varargin})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
377 ## Parses and validates list of arguments according to object @var{parser} of
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
378 ## the class inputParser.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
379 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
380 ## After parsing, the results can be accessed with the @code{Results}
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
381 ## accessor. See @code{help inputParser} for a more complete description.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
382 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
383 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 28187
diff changeset
384
22541
4b7ab10b5c38 inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22489
diff changeset
385 this.Results = struct ();
22559
27f2a7a3b55d inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22541
diff changeset
386 this.Unmatched = struct ();
27f2a7a3b55d inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22541
diff changeset
387 this.UsingDefaults = cell ();
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
388 if (numel (varargin) < numel (this.Required))
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
389 if (this.FunctionName)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
390 print_usage (this.FunctionName);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
391 else
20726
25d676f9619c Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents: 20575
diff changeset
392 this.error ("inputParser.parse: not enough input arguments");
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
393 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
394 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
395 pnargin = numel (varargin);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
396
23167
a2d6b84ce5c5 scripts/general/inputParser.m: refactor two xtest cases to normal test cases.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23165
diff changeset
397 this.ParameterNames = fieldnames (this.Parameter);
a2d6b84ce5c5 scripts/general/inputParser.m: refactor two xtest cases to normal test cases.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23165
diff changeset
398 this.SwitchNames = fieldnames (this.Switch);
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
399
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
400 ## Evaluate the Required arguments first
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
401 nReq = numel (this.Required);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
402 for idx = 1:nReq
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
403 req = this.Required{idx};
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
404 this.validate_arg (req.name, req.val, varargin{idx});
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
405 endfor
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
406
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
407 vidx = nReq; # current index in varargin
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
408
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
409 ## Search for a list of Optional arguments
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
410 idx = 0; # current index on the array of Optional
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
411 nOpt = numel (this.Optional);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
412 while (vidx < pnargin && idx < nOpt)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
413 opt = this.Optional{++idx};
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
414 in = varargin{++vidx};
23377
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
415 if ((this.is_argname ("Parameter", in) && vidx < pnargin)
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
416 || this.is_argname ("Switch", in))
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
417 ## This looks like an optional parameter/value pair or a
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
418 ## switch, not an positional option. This does mean that
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
419 ## positional options cannot be strings named like parameter
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
420 ## keys. See bug #50752.
23365
7b594fcfa32b improve inputParser compatibility for optional args (bug #50752)
John W. Eaton <jwe@octave.org>
parents: 23337
diff changeset
421 idx -= 1;
7b594fcfa32b improve inputParser compatibility for optional args (bug #50752)
John W. Eaton <jwe@octave.org>
parents: 23337
diff changeset
422 vidx -= 1;
28917
72d57dbcc305 maint: Add semicolon after break and return keywords.
Rik <rik@octave.org>
parents: 28912
diff changeset
423 break;
23365
7b594fcfa32b improve inputParser compatibility for optional args (bug #50752)
John W. Eaton <jwe@octave.org>
parents: 23337
diff changeset
424 endif
21666
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
425 try
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
426 valid_option = opt.val (in);
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
427 catch
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
428 valid_option = false;
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
429 end_try_catch
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
430 if (! valid_option)
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
431 ## If it does not match there's two options:
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
432 ## 1) input is actually wrong and we should error;
23377
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
433 ## 2) it's a Parameter or Switch name and we should use
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
434 ## the default for the rest;
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
435 ## 3) it's a struct with the Parameter pairs.
21667
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
436 if (ischar (in) || (this.StructExpand && isstruct (in)
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
437 && isscalar (in)))
20735
418ae0cb752f Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents: 20726
diff changeset
438 idx -= 1;
418ae0cb752f Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents: 20726
diff changeset
439 vidx -= 1;
28917
72d57dbcc305 maint: Add semicolon after break and return keywords.
Rik <rik@octave.org>
parents: 28912
diff changeset
440 break;
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
441 else
23165
5291b67ff124 More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents: 23164
diff changeset
442 this.error (sprintf (["failed validation of %s\n", ...
5291b67ff124 More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents: 23164
diff changeset
443 "Validation function: %s"],
28912
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 28892
diff changeset
444 toupper (opt.name), disp (opt.val)));
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
445 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
446 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
447 this.Results.(opt.name) = in;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
448 endwhile
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
449
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
450 ## Fill in with defaults of missing Optional
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
451 while (idx++ < nOpt)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
452 opt = this.Optional{idx};
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
453 this.UsingDefaults{end+1} = opt.name;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
454 this.Results.(opt.name) = opt.def;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
455 endwhile
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
456
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
457 ## Search unordered Options (Switch and Parameter)
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
458 while (vidx++ < pnargin)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
459 name = varargin{vidx};
21667
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
460
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
461 if (this.StructExpand && isstruct (name) && isscalar (name))
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
462 expanded_options = [fieldnames(name) struct2cell(name)]'(:);
25628
a0d642f24349 Parse empty struct input correctly (bug #54318).
Rik <rik@octave.org>
parents: 25054
diff changeset
463 if (isempty (expanded_options))
a0d642f24349 Parse empty struct input correctly (bug #54318).
Rik <rik@octave.org>
parents: 25054
diff changeset
464 continue; # empty, continue to next argument
a0d642f24349 Parse empty struct input correctly (bug #54318).
Rik <rik@octave.org>
parents: 25054
diff changeset
465 endif
21667
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
466 n_new_args = numel (expanded_options) -1;
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
467 pnargin += n_new_args;
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
468 varargin(vidx+n_new_args+1:pnargin) = varargin(vidx+1:end);
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
469 varargin(vidx:vidx+n_new_args) = expanded_options;
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
470 name = varargin{vidx};
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
471 endif
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
472
23383
62320e943696 inputParser.m: check if Param key and Switch are strings for better errors.
Carnë Draug <carandraug@octave.org>
parents: 23382
diff changeset
473 if (! ischar (name))
62320e943696 inputParser.m: check if Param key and Switch are strings for better errors.
Carnë Draug <carandraug@octave.org>
parents: 23382
diff changeset
474 this.error ("non-string for Parameter name or Switch");
62320e943696 inputParser.m: check if Param key and Switch are strings for better errors.
Carnë Draug <carandraug@octave.org>
parents: 23382
diff changeset
475 endif
62320e943696 inputParser.m: check if Param key and Switch are strings for better errors.
Carnë Draug <carandraug@octave.org>
parents: 23382
diff changeset
476
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
477 if (this.is_argname ("Parameter", name))
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
478 if (vidx++ > pnargin)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
479 this.error (sprintf ("no matching value for option '%s'",
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
480 toupper (name)));
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
481 endif
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19597
diff changeset
482 this.validate_arg (this.last_name,
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
483 this.Parameter.(this.last_name).val,
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
484 varargin{vidx});
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
485 elseif (this.is_argname ("Switch", name))
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
486 this.Results.(this.last_name) = true;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
487 else
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
488 if (vidx++ < pnargin && this.KeepUnmatched)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
489 this.Unmatched.(name) = varargin{vidx};
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
490 else
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
491 this.error (sprintf ("argument '%s' is not a valid parameter",
23167
a2d6b84ce5c5 scripts/general/inputParser.m: refactor two xtest cases to normal test cases.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23165
diff changeset
492 toupper (name)));
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
493 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
494 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
495 endwhile
23337
f04f32f08590 Override disp rather than display for class objects (bug #50640).
Rik <rik@octave.org>
parents: 23333
diff changeset
496 ## Add them to the UsingDefaults list
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
497 this.add_missing ("Parameter");
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
498 this.add_missing ("Switch");
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
499
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
500 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
501
23337
f04f32f08590 Override disp rather than display for class objects (bug #50640).
Rik <rik@octave.org>
parents: 23333
diff changeset
502 function disp (this)
23333
b636f10ce53f fix display methods to only expect one argument
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
503 if (nargin != 1)
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
504 print_usage ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
505 endif
23337
f04f32f08590 Override disp rather than display for class objects (bug #50640).
Rik <rik@octave.org>
parents: 23333
diff changeset
506 printf ("inputParser object with properties:\n\n");
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
507 b2s = @(x) ifelse (any (x), "true", "false");
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
508 printf ([" CaseSensitive : %s\n FunctionName : %s\n" ...
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
509 " KeepUnmatched : %s\n PartialMatching : %s\n" ...
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
510 " StructExpand : %s\n\n"],
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
511 b2s (this.CaseSensitive), b2s (this.FunctionName),
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
512 b2s (this.KeepUnmatched), b2s (this.PartialMatching),
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
513 b2s (this.StructExpand));
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
514 printf ("Defined parameters:\n\n {%s}\n",
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
515 strjoin (this.Parameters, ", "));
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
516 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
517 endmethods
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
518
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
519 methods (Access = private)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
520 function validate_name (this, type, name)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
521 if (! isvarname (name))
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
522 error ("inputParser.add%s: NAME is an invalid identifier", method);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
523 elseif (any (strcmpi (this.Parameters, name)))
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
524 ## Even if CaseSensitive is "on", we still shouldn't allow
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
525 ## two args with the same name.
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
526 error ("inputParser.add%s: argname '%s' has already been specified",
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
527 type, name);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
528 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
529 this.Parameters{end+1} = name;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
530 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
531
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
532 function validate_arg (this, name, val, in)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
533 if (! val (in))
23165
5291b67ff124 More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents: 23164
diff changeset
534 this.error (sprintf ("failed validation of %s with %s",
5291b67ff124 More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents: 23164
diff changeset
535 toupper (name), func2str (val)));
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
536 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
537 this.Results.(name) = in;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
538 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
539
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
540 function r = is_argname (this, type, name)
28187
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
541 r = ischar (name) && isrow (name);
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
542 if (r)
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
543 if (this.CaseSensitive)
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
544 r = isfield (this.(type), name);
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
545 if (r)
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
546 this.last_name = name;
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
547 endif
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
548 else
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
549 fnames = this.([type "Names"]);
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
550 l = strcmpi (name, fnames);
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
551 r = any (l(:));
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
552 if (r)
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
553 this.last_name = fnames{l};
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
554 endif
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
555 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
556 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
557 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
558
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
559 function add_missing (this, type)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
560 unmatched = setdiff (fieldnames (this.(type)), fieldnames (this.Results));
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
561 for namec = unmatched(:)'
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
562 name = namec{1};
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
563 this.UsingDefaults{end+1} = name;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
564 this.Results.(name) = this.(type).(name).def;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
565 endfor
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
566 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
567
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
568 function error (this, msg)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
569 where = "";
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
570 if (this.FunctionName)
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
571 where = [this.FunctionName ": "];
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
572 endif
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
573 error ("%s%s", where, msg);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
574 endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
575 endmethods
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
576
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
577 endclassdef
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
578
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
579 %!function p = create_p ()
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
580 %! p = inputParser ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
581 %! p.CaseSensitive = true;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
582 %! p.addRequired ("req1", @(x) ischar (x));
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
583 %! p.addOptional ("op1", "val", @(x) any (strcmp (x, {"val", "foo"})));
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
584 %! p.addOptional ("op2", 78, @(x) x > 50);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
585 %! p.addSwitch ("verbose");
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
586 %! p.addParameter ("line", "tree", @(x) any (strcmp (x, {"tree", "circle"})));
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
587 %!endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
588
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
589 ## check normal use, only required are given
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
590 %!test
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
591 %! p = create_p ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
592 %! p.parse ("file");
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
593 %! r = p.Results;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
594 %! assert (r.req1, "file");
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
595 %! assert (sort (p.UsingDefaults), sort ({"op1", "op2", "verbose", "line"}));
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
596 %! assert ({r.req1, r.op1, r.op2, r.verbose, r.line},
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21578
diff changeset
597 %! {"file", "val", 78, false, "tree"});
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
598
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
599 ## check normal use, but give values different than defaults
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
600 %!test
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
601 %! p = create_p ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
602 %! p.parse ("file", "foo", 80, "line", "circle", "verbose");
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
603 %! r = p.Results;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
604 %! assert ({r.req1, r.op1, r.op2, r.verbose, r.line},
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
605 %! {"file", "foo", 80, true, "circle"});
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
606
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
607 ## check optional is skipped and considered Parameter if unvalidated string
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
608 %!test
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
609 %! p = create_p ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
610 %! p.parse ("file", "line", "circle");
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
611 %! r = p.Results;
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19039
diff changeset
612 %! assert ({r.req1, r.op1, r.op2, r.verbose, r.line},
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
613 %! {"file", "val", 78, false, "circle"});
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
614
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
615 ## check case insensitivity
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
616 %!test
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
617 %! p = create_p ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
618 %! p.CaseSensitive = false;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
619 %! p.parse ("file", "foo", 80, "LiNE", "circle", "vERbOSe");
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
620 %! r = p.Results;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
621 %! assert ({r.req1, r.op1, r.op2, r.verbose, r.line},
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
622 %! {"file", "foo", 80, true, "circle"});
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
623
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
624 ## check KeepUnmatched
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
625 %!test
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
626 %! p = create_p ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
627 %! p.KeepUnmatched = true;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
628 %! p.parse ("file", "foo", 80, "line", "circle", "verbose", "extra", 50);
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21578
diff changeset
629 %! assert (p.Unmatched.extra, 50);
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
630
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
631 ## check error when missing required
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
632 %!error <not enough input arguments>
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
633 %! p = create_p ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
634 %! p.parse ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
635
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
636 ## check error when given required does not validate
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
637 %!error <failed validation of >
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
638 %! p = create_p ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
639 %! p.parse (50);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
640
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
641 ## check error when given optional does not validate
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
642 %!error <is not a valid parameter>
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
643 %! p = create_p ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
644 %! p.parse ("file", "no-val");
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
645
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
646 ## check error when given Parameter does not validate
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
647 %!error <failed validation of >
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
648 %! p = create_p ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
649 %! p.parse ("file", "foo", 51, "line", "round");
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
650
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
651 ## check alternative method (obj, ...) API
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
652 %!function p2 = create_p2 ();
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
653 %! p2 = inputParser ();
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
654 %! addRequired (p2, "req1", @(x) ischar (x));
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
655 %! addOptional (p2, "op1", "val", @(x) any (strcmp (x, {"val", "foo"})));
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
656 %! addOptional (p2, "op2", 78, @(x) x > 50);
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
657 %! addSwitch (p2, "verbose");
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
658 %! addParameter (p2, "line", "tree", @(x) any (strcmp (x, {"tree", "circle"})));
19035
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
659 %!endfunction
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
660
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
661 ## check normal use, only required are given
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
662 %!test
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
663 %! p2 = create_p2 ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
664 %! parse (p2, "file");
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
665 %! r = p2.Results;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
666 %! assert ({r.req1, r.op1, r.op2, r.verbose, r.line},
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
667 %! {"file", "val", 78, false, "tree"});
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
668 %! assert (sort (p2.UsingDefaults), sort ({"op1", "op2", "verbose", "line"}));
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
669
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
670 ## check normal use, but give values different than defaults
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
671 %!test
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
672 %! p2 = create_p2 ();
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
673 %! parse (p2, "file", "foo", 80, "line", "circle", "verbose");
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
674 %! r = p2.Results;
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
675 %! assert ({r.req1, r.op1, r.op2, r.verbose, r.line},
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
676 %! {"file", "foo", 80, true, "circle"});
ff820f92cbb5 inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
677
22489
93ea313301f9 test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents: 22323
diff changeset
678 ## We must not perform validation of default values
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
679 %!test <*45837>
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
680 %! p = inputParser ();
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
681 %! p.addParameter ("Dir", [], @ischar);
20575
a61f0d6beb71 inputParser: do not perform validation of default values (bug #45837)
Carnë Draug <carandraug@octave.org>
parents: 20470
diff changeset
682 %! p.parse ();
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21578
diff changeset
683 %! assert (p.Results.Dir, []);
20575
a61f0d6beb71 inputParser: do not perform validation of default values (bug #45837)
Carnë Draug <carandraug@octave.org>
parents: 20470
diff changeset
684
a61f0d6beb71 inputParser: do not perform validation of default values (bug #45837)
Carnë Draug <carandraug@octave.org>
parents: 20470
diff changeset
685 %!test
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
686 %! p = inputParser ();
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
687 %! p.addParameter ("positive", -1, @(x) x > 5);
20575
a61f0d6beb71 inputParser: do not perform validation of default values (bug #45837)
Carnë Draug <carandraug@octave.org>
parents: 20470
diff changeset
688 %! p.parse ();
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21578
diff changeset
689 %! assert (p.Results.positive, -1);
20575
a61f0d6beb71 inputParser: do not perform validation of default values (bug #45837)
Carnë Draug <carandraug@octave.org>
parents: 20470
diff changeset
690
21666
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
691 ## Throw an error on validation of optional argument to check that it
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
692 ## is caught without preventing continuation into param/value pairs.
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
693 %!test
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
694 %! p = inputParser ();
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
695 %! p.addOptional ("err", "foo", @error);
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
696 %! p.addParameter ("not_err", "bar", @ischar);
21666
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
697 %! p.parse ("not_err", "qux");
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
698 %! assert (p.Results.err, "foo")
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
699 %! assert (p.Results.not_err, "qux")
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
700
09517e0a8aa0 inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents: 21634
diff changeset
701
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
702 ## With more Parameters to test StructExpand
21667
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
703 %!function p3 = create_p3 ();
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
704 %! p3 = inputParser ();
21667
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
705 %! addOptional (p3, "op1", "val", @(x) any (strcmp (x, {"val", "foo"})));
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
706 %! addOptional (p3, "op2", 78, @(x) x > 50);
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
707 %! addSwitch (p3, "verbose");
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
708 %! addParameter (p3, "line", "tree", @(x) any (strcmp (x, {"tree", "circle"})));
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
709 %! addParameter (p3, "color", "red", @(x) any (strcmp (x, {"red", "green"})));
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
710 %! addParameter (p3, "style", "tt", @(x) any (strcmp (x, {"tt", "f", "i"})));
21667
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
711 %!endfunction
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
712
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
713 ## Test StructExpand
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
714 %!test
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
715 %! p3 = create_p3 ();
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
716 %! p3.parse (struct ("line", "circle", "color", "green"));
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
717 %! assert (p3.Results, struct ("op1", "val", "op2", 78, "verbose", false,
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
718 %! "line", "circle", "color", "green",
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
719 %! "style", "tt"))
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
720
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
721 %!test
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
722 %! p3 = create_p3 ();
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
723 %! p3.parse (struct ("line", "circle", "color", "green"), "line", "tree");
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
724 %! assert (p3.Results.line, "tree")
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
725 %! p3.parse ("line", "tree", struct ("line", "circle", "color", "green"));
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
726 %! assert (p3.Results.line, "circle")
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
727
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
728 %!test # unmatched parameters with StructExpand
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
729 %! p3 = create_p3 ();
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
730 %! p3.KeepUnmatched = true;
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
731 %! p3.parse (struct ("line", "circle", "color", "green", "bar", "baz"));
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
732 %! assert (p3.Unmatched.bar, "baz")
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
733
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
734 ## The validation for the second optional argument throws an error with
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
735 ## a struct so check that we can handle it.
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
736 %!test
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
737 %! p3 = create_p3 ();
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
738 %! p3.parse ("foo", struct ("color", "green"), "line", "tree");
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
739 %! assert (p3.Results.op1, "foo")
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
740 %! assert (p3.Results.line, "tree")
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
741 %! assert (p3.Results.color, "green")
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
742 %! assert (p3.Results.verbose, false)
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
743
84092ccb45e2 inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents: 21666
diff changeset
744
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
745 ## Some simple tests for addParamValue since all the other ones use add
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
746 ## addParameter but they use the same codepath.
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
747 %!test
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
748 %! p = inputParser ();
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
749 %! addParameter (p, "line", "tree", @(x) any (strcmp (x, {"tree", "circle"})));
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
750 %! addParameter (p, "color", "red", @(x) any (strcmp (x, {"red", "green"})));
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
751 %! p.parse ("line", "circle");
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
752 %! assert ({p.Results.line, p.Results.color}, {"circle", "red"})
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
753
23167
a2d6b84ce5c5 scripts/general/inputParser.m: refactor two xtest cases to normal test cases.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23165
diff changeset
754 %!test
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
755 %! p = inputParser ();
23167
a2d6b84ce5c5 scripts/general/inputParser.m: refactor two xtest cases to normal test cases.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23165
diff changeset
756 %! p.addParameter ("foo", "bar", @ischar);
a2d6b84ce5c5 scripts/general/inputParser.m: refactor two xtest cases to normal test cases.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23165
diff changeset
757 %! p.parse ();
a2d6b84ce5c5 scripts/general/inputParser.m: refactor two xtest cases to normal test cases.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23165
diff changeset
758 %! assert (p.Results, struct ("foo", "bar"))
a2d6b84ce5c5 scripts/general/inputParser.m: refactor two xtest cases to normal test cases.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23165
diff changeset
759 %! p.parse ("foo", "qux");
a2d6b84ce5c5 scripts/general/inputParser.m: refactor two xtest cases to normal test cases.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23165
diff changeset
760 %! assert (p.Results, struct ("foo", "qux"))
21508
04923b7b0d89 inputParser.m: add failing test due to bug in the parser (bug #44779)
Carnë Draug <carandraug@octave.org>
parents: 21505
diff changeset
761
23377
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
762 ## This behaviour means that a positional option can never be a string
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
763 ## that is the name of a parameter key. This is required for Matlab
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
764 ## compatibility.
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
765 %!test <*50752>
23382
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
766 %! p = inputParser ();
21505
7f7d7cb73e0d inputParser.m: uncomment failing test and use %!xtest instead.
Carnë Draug <carandraug@octave.org>
parents: 21316
diff changeset
767 %! p.addOptional ("op1", "val");
21677
1cd04fb745f6 inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents: 21668
diff changeset
768 %! p.addParameter ("line", "tree");
21505
7f7d7cb73e0d inputParser.m: uncomment failing test and use %!xtest instead.
Carnë Draug <carandraug@octave.org>
parents: 21316
diff changeset
769 %! p.parse ("line", "circle");
23377
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
770 %! assert (p.Results, struct ("op1", "val", "line", "circle"))
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
771 %!
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
772 %! p = inputParser ();
23382
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
773 %! p.addOptional ("op1", "val1");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
774 %! p.addOptional ("op2", "val2");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
775 %! p.addParameter ("line", "tree");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
776 %! p.parse ("line", "circle");
23377
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
777 %! assert (p.Results.op1, "val1")
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
778 %! assert (p.Results.op2, "val2")
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
779 %! assert (p.Results.line, "circle")
23382
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
780 %!
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
781 %! ## If there's enough arguments to fill the positional options and
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
782 %! ## param/key, it still skips positional options.
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
783 %! p = inputParser ();
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
784 %! p.addOptional ("op1", "val1");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
785 %! p.addOptional ("op2", "val2");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
786 %! p.addParameter ("line", "tree");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
787 %! p.parse ("line", "circle", "line", "rectangle");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
788 %! assert (p.Results, struct ("op1", "val1", "op2", "val2",
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
789 %! "line", "rectangle"))
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
790 %!
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
791 %! ## Even if the key/param fails validation, it does not backtrack to
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
792 %! ## check if the values are valid positional options.
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
793 %! p = inputParser ();
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
794 %! p.addOptional ("op1", "val1", @ischar);
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
795 %! p.addOptional ("op2", "val2", @isnumeric);
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
796 %! p.addParameter ("line", "circle", @ischar);
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
797 %! fail ('p.parse ("line", 89)', "failed validation of LINE")
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
798 %!
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
799 %! p = inputParser ();
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
800 %! p.addOptional ("op1", "val1");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
801 %! p.addParamValue ("line", "circle", @ischar);
23383
62320e943696 inputParser.m: check if Param key and Switch are strings for better errors.
Carnë Draug <carandraug@octave.org>
parents: 23382
diff changeset
802 %! fail ('p.parse ("line", "line", 89)',
62320e943696 inputParser.m: check if Param key and Switch are strings for better errors.
Carnë Draug <carandraug@octave.org>
parents: 23382
diff changeset
803 %! "non-string for Parameter name or Switch")
23382
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
804
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
805 %!test <*50752>
23382
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
806 %! ## This fails in Matlab but works in Octave. It is a bug there
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
807 %! ## that we do not replicate.
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
808 %! p = inputParser ();
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
809 %! p.addOptional ("op1", "val1");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
810 %! p.addParameter ("line", "circle");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
811 %! p.parse ("line");
5a3c3ff03167 inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23377
diff changeset
812 %! assert (p.Results, struct ("op1", "line", "line", "circle"))
23377
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
813
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
814 %!test <*50752>
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
815 %! p = inputParser ();
23377
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
816 %! p.addOptional ("op1", "val1");
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
817 %! p.addSwitch ("line");
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
818 %! p.parse ("line");
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
819 %! assert (p.Results.op1, "val1")
f1bf2590272a inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents: 23365
diff changeset
820 %! assert (p.Results.line, true)
22541
4b7ab10b5c38 inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22489
diff changeset
821
4b7ab10b5c38 inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22489
diff changeset
822 %!test
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
823 %! p = inputParser ();
22541
4b7ab10b5c38 inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22489
diff changeset
824 %! p.addParameter ("a", []);
4b7ab10b5c38 inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22489
diff changeset
825 %! p.addParameter ("b", []);
4b7ab10b5c38 inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22489
diff changeset
826 %! p.parse ("a", 1);
4b7ab10b5c38 inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22489
diff changeset
827 %! p.parse ("b", 1);
4b7ab10b5c38 inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22489
diff changeset
828 %! assert (p.Results, struct ("a", [], "b", 1));
22559
27f2a7a3b55d inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22541
diff changeset
829 %! assert (p.UsingDefaults, {"a"});
22541
4b7ab10b5c38 inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22489
diff changeset
830
22559
27f2a7a3b55d inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22541
diff changeset
831 %!test
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
832 %! p = inputParser ();
22559
27f2a7a3b55d inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22541
diff changeset
833 %! p.addParameter ("b", []);
27f2a7a3b55d inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22541
diff changeset
834 %! p.KeepUnmatched = true;
27f2a7a3b55d inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22541
diff changeset
835 %! p.parse ("a", 1);
27f2a7a3b55d inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22541
diff changeset
836 %! p.parse ("b", 1);
27f2a7a3b55d inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22541
diff changeset
837 %! assert (p.Results, struct ("b", 1));
27f2a7a3b55d inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents: 22541
diff changeset
838 %! assert (p.Unmatched, struct ());
23165
5291b67ff124 More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents: 23164
diff changeset
839
5291b67ff124 More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents: 23164
diff changeset
840 ## Test for patch #9241
5291b67ff124 More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents: 23164
diff changeset
841 %!error<failed validation of A with ischar>
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
842 %! p = inputParser ();
23165
5291b67ff124 More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents: 23164
diff changeset
843 %! p.addParameter ("a", [], @ischar);
5291b67ff124 More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents: 23164
diff changeset
844 %! p.parse ("a", 1);
28187
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
845
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
846 %!test <*58112>
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
847 %! p = inputParser ();
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
848 %! p.addRequired ("first");
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
849 %! p.addOptional ("second", []);
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
850 %! p.parse (1, {"test", 1, 2, 3});
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
851 %! r = p.Results;
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
852 %! assert (r.first, 1);
3faa700d64d4 inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents: 27978
diff changeset
853 %! assert (r.second, {"test", 1, 2, 3});