Mercurial > octave
annotate scripts/miscellaneous/inputParser.m @ 31103:7797481038fc stable
inputParser: Add some tests for bug #62639.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Tue, 21 Jun 2022 15:43:27 +0200 |
parents | c415b218307f |
children | c8ad083a5802 |
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 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30379
diff
changeset
|
3 ## Copyright (C) 2011-2022 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. |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
48 ## @end deftypefn |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
49 ## |
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. |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
52 ## @end deftypefn |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
53 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
54 ## @deftypefn {} {} inputParser.Results |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
55 ## Return structure with argument names as fieldnames and corresponding |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
56 ## values. |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
57 ## @end deftypefn |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
58 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
59 ## @deftypefn {} {} inputParser.Unmatched |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
60 ## 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
|
61 ## See the @code{KeepUnmatched} property. |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
62 ## @end deftypefn |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
63 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
64 ## @deftypefn {} {} inputParser.UsingDefaults |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
65 ## Return cell array with the names of arguments that are using default |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
66 ## values. |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
67 ## @end deftypefn |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
68 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
69 ## @deftypefn {} {} inputParser.CaseSensitive = @var{boolean} |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
70 ## 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
|
71 ## to false. |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
72 ## @end deftypefn |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
73 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
74 ## @deftypefn {} {} inputParser.FunctionName = @var{name} |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
75 ## Set function name to be used in error messages; Defaults to empty string. |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
76 ## @end deftypefn |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
77 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
78 ## @deftypefn {} {} inputParser.KeepUnmatched = @var{boolean} |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
79 ## Set whether an error should be given for non-defined arguments. Defaults |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
80 ## to false. If set to true, the extra arguments can be accessed through |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
81 ## @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
|
82 ## @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
|
83 ## 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
|
84 ## 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
|
85 ## be followed by a value. |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
86 ## @end deftypefn |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
87 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
88 ## @deftypefn {} {} inputParser.StructExpand = @var{boolean} |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
89 ## 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
|
90 ## parameter/value pairs. Defaults to true. |
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 ## 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
|
93 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
94 ## @example |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
95 ## function check (varargin) |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
96 ## @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
|
97 ## p = inputParser (); # create object |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
98 ## p.FunctionName = "check"; # set function name |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
99 ## p.addRequired ("pack", @@ischar); # mandatory argument |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
100 ## 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
|
101 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
102 ## ## 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
|
103 ## 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
|
104 ## 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
|
105 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
106 ## ## create two arguments of type "Parameter" |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
107 ## 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
|
108 ## p.addParameter ("type", "linear", val_type); |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
109 ## 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
|
110 ## p.addParameter ("tolerance", "low", val_verb); |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
111 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
112 ## ## create a switch type of argument |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
113 ## p.addSwitch ("verbose"); |
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 ## 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
|
116 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
117 ## ## 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
|
118 ## ## 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
|
119 ## endfunction |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
120 ## @end example |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
121 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
122 ## @example |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
123 ## @group |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
124 ## 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
|
125 ## check (); # error, one argument is mandatory |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
126 ## check (1); # error, since ! ischar |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
127 ## 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
|
128 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
129 ## 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
|
130 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
131 ## ## 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
|
132 ## ## 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
|
133 ## ## 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
|
134 ## 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
|
135 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
136 ## ## 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
|
137 ## ## '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
|
138 ## check ("mech", "~/dev", "type", "linear"); |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
139 ## @end group |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
140 ## @end example |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
141 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
142 ## @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
|
143 ## 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
|
144 ## 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
|
145 ## 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
|
146 ## 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
|
147 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
148 ## @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
|
149 ## 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
|
150 ## 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
|
151 ## 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
|
152 ## @code{Parameter} or @code{Switch} arguments. |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
153 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
154 ## @seealso{nargin, validateattributes, validatestring, varargin} |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
155 ## @end deftypefn |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
156 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
157 properties |
21578
683a1beee538
maint: Use "FIXME:" for all code blocks needing further attention.
Rik <rik@octave.org>
parents:
21568
diff
changeset
|
158 ## FIXME: set input checking for these properties |
21568
3d60ed163b70
maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
159 CaseSensitive = false; |
3d60ed163b70
maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
160 FunctionName = ""; |
3d60ed163b70
maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
161 KeepUnmatched = false; |
21937
55f7de37b618
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
162 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
|
163 StructExpand = true; |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
164 endproperties |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
165 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
166 properties (SetAccess = protected) |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19597
diff
changeset
|
167 Parameters = cell (); |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19597
diff
changeset
|
168 Results = struct (); |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19597
diff
changeset
|
169 Unmatched = struct (); |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19597
diff
changeset
|
170 UsingDefaults = cell (); |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
171 endproperties |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
172 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
173 properties (Access = protected) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
174 ## 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
|
175 ## 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
|
176 Required = cell (); |
99ca557fd34f
maint: Use Octave whitespace conventions for scripts/general/inputParser.m.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
22755
diff
changeset
|
177 Optional = cell (); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
178 ## 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
|
179 ## 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
|
180 Parameter = struct (); |
99ca557fd34f
maint: Use Octave whitespace conventions for scripts/general/inputParser.m.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
22755
diff
changeset
|
181 Switch = struct (); |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
182 |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
183 ## 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
|
184 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
|
185 SwitchNames = cell (); |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
186 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
187 ## 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
|
188 ## 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
|
189 ## is_argname method. |
21568
3d60ed163b70
maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
190 last_name = ""; |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
191 endproperties |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
192 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
193 properties (Access = protected, Constant = true) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
194 ## 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
|
195 def_val = @(~) true; |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
196 endproperties |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
197 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
198 methods |
28945
6e460773bdda
maint: Use newlines after "function" and before "endfunction" for clarity.
Rik <rik@octave.org>
parents:
28929
diff
changeset
|
199 |
21668
58f5a6347851
inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents:
21667
diff
changeset
|
200 function set.PartialMatching (this, val) |
58f5a6347851
inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents:
21667
diff
changeset
|
201 if (val) |
58f5a6347851
inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents:
21667
diff
changeset
|
202 error ("inputParser: PartialMatching is not yet implemented"); |
58f5a6347851
inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents:
21667
diff
changeset
|
203 endif |
58f5a6347851
inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents:
21667
diff
changeset
|
204 endfunction |
58f5a6347851
inpputParser.m: fix display method.
Carnë Draug <carandraug@octave.org>
parents:
21667
diff
changeset
|
205 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
206 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
|
207 |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
208 ## -*- texinfo -*- |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
209 ## @deftypefn {} {} addRequired (@var{argname}) |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
210 ## @deftypefnx {} {} addRequired (@var{argname}, @var{validator}) |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
211 ## Add new mandatory argument to the object @var{parser} of inputParser |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
212 ## class. This method belongs to the inputParser class and implements |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
213 ## an ordered arguments type of API. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
214 ## |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
215 ## @var{argname} must be a string with the name of the new argument. The |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
216 ## order in which new arguments are added with @code{addrequired}, |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
217 ## represents the expected order of arguments. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
218 ## |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
219 ## @var{validator} is an optional function handle to validate the given |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
220 ## values for the argument with name @var{argname}. Alternatively, a |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
221 ## function name can be used. |
28378
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 ## See @code{help inputParser} for examples. |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
224 ## |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
225 ## @emph{Note}: this can be used together with the other type of |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
226 ## arguments but it must be the first (see @code{@@inputParser}). |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
227 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
228 ## @end deftypefn |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
229 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
28563
diff
changeset
|
230 if (nargin < 2) |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
231 print_usage (); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
232 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
|
233 || numfields (this.Switch)) |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
234 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
|
235 "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
|
236 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
237 this.validate_name ("Required", name); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
238 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
|
239 endfunction |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
240 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
241 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
|
242 |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
243 ## -*- texinfo -*- |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
244 ## @deftypefn {} {} addOptional (@var{argname}, @var{default}) |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
245 ## @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
|
246 ## 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
|
247 ## 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
|
248 ## |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
249 ## @var{argname} must be a string with the name of the new argument. The |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
250 ## order in which new arguments are added with @code{addOptional}, |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
251 ## represents the expected order of arguments. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
252 ## |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
253 ## @var{default} will be the value used when the argument is not |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
254 ## specified. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
255 ## |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
256 ## @var{validator} is an optional anonymous function to validate the |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
257 ## given values for the argument with name @var{argname}. Alternatively, |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
258 ## a function name can be used. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
259 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
260 ## See @code{help inputParser} for examples. |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
261 ## |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
262 ## @emph{Note}: if a string argument does not validate, it will be |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
263 ## considered a ParamValue key. If an optional argument is not given a |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
264 ## validator, anything will be valid, and so any string will be |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
265 ## considered will be the value of the optional argument (in @sc{matlab}, |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
266 ## if no validator is given and argument is a string it will also be |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
267 ## considered a ParamValue key). |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
268 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
269 ## @end deftypefn |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
270 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
28563
diff
changeset
|
271 if (nargin < 3) |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
272 print_usage (); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
273 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
|
274 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
|
275 "after Parameter or Switch"]); |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
276 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
277 this.validate_name ("Optional", name); |
31101
c415b218307f
inputParser: Correctly handle cell default values for optional parameters (bug #62639).
Markus Mützel <markus.muetzel@gmx.de>
parents:
31074
diff
changeset
|
278 if (iscell (def)) |
c415b218307f
inputParser: Correctly handle cell default values for optional parameters (bug #62639).
Markus Mützel <markus.muetzel@gmx.de>
parents:
31074
diff
changeset
|
279 def = {def}; |
c415b218307f
inputParser: Correctly handle cell default values for optional parameters (bug #62639).
Markus Mützel <markus.muetzel@gmx.de>
parents:
31074
diff
changeset
|
280 endif |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
281 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
|
282 endfunction |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
283 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
284 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
|
285 |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
286 ## -*- texinfo -*- |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
287 ## @deftypefn {} {} addParamValue (@var{argname}, @var{default}) |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
288 ## @deftypefnx {} {} addParamValue (@var{argname}, @var{default}, @var{validator}) |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
289 ## Add new parameter to the object @var{parser} of the class inputParser |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
290 ## to implement a name/value pair type of API. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
291 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
292 ## 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
|
293 ## @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
|
294 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
295 ## @end deftypefn |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
296 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
28563
diff
changeset
|
297 if (nargin < 3) |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
298 print_usage (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
299 endif |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
300 this.addParameter (name, def, val); |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
301 endfunction |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
302 |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
303 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
|
304 |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
305 ## -*- texinfo -*- |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
306 ## @deftypefn {} {} addParameter (@var{argname}, @var{default}) |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
307 ## @deftypefnx {} {} addParameter (@var{argname}, @var{default}, @var{validator}) |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
308 ## Add new parameter to the object @var{parser} of the class inputParser |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
309 ## to implement a name/value pair type of API. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
310 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
311 ## @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
|
312 ## |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
313 ## @var{default} will be the value used when the parameter is not |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
314 ## specified. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
315 ## |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
316 ## @var{validator} is an optional function handle to validate the given |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
317 ## values for the parameter with name @var{argname}. Alternatively, a |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
318 ## function name can be used. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
319 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
320 ## See @code{help inputParser} for examples. |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
321 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
322 ## @end deftypefn |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
323 |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
324 if (nargin < 3 || nargin > 6) |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
325 print_usage (); |
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 n_opt = numel (varargin); |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
329 |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
330 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
|
331 val = inputParser.def_val; |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
332 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
|
333 val = varargin{1}; |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
334 endif |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
335 |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
336 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
|
337 match_priority = 1; |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
338 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
|
339 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
|
340 error ("inputParser.addParameter: unrecognized option"); |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
341 endif |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
342 match_priority = varargin{end}; |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
343 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
|
344 "inputParser.addParameter", |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
345 "PartialMatchPriority"); |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
346 endif |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
347 |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
348 this.validate_name ("Parameter", name); |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
349 this.Parameter.(name).def = def; |
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
350 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
|
351 endfunction |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
352 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
353 function addSwitch (this, name) |
28378
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 ## -*- texinfo -*- |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
356 ## @deftypefn {} {} addSwitch (@var{argname}) |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
357 ## Add new switch type of argument to the object @var{parser} of |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
358 ## inputParser class. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
359 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
360 ## 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
|
361 ## arguments type of API. |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
362 ## |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
363 ## @var{argname} must be a string with the name of the new argument. |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
364 ## Arguments of this type can be specified at the end, after |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
365 ## @code{Required} and @code{Optional}, and mixed between the |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
366 ## @code{Parameter}. They default to false. If one of the arguments |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
367 ## supplied is a string like @var{argname}, then after parsing the value |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
368 ## of @var{parse}.Results.@var{argname} will be true. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
369 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
370 ## See @code{help inputParser} for examples. |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
371 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
372 ## @end deftypefn |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
373 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
374 if (nargin != 2) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
375 print_usage (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
376 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
377 this.validate_name ("Switch", name); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
378 this.Switch.(name).def = false; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
379 endfunction |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
380 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
381 function parse (this, varargin) |
28378
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 ## -*- texinfo -*- |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
384 ## @deftypefn {} {} parse (@var{varargin}) |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
385 ## Parses and validates list of arguments according to object |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
386 ## @var{parser} of the class inputParser. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
387 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
388 ## After parsing, the results can be accessed with the @code{Results} |
30353
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
389 ## accessor. See @code{help inputParser} for a more complete |
212461a26e9c
doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
390 ## description. |
28378
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
391 ## |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
392 ## @end deftypefn |
8dbe4eec1741
move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents:
28187
diff
changeset
|
393 |
22541
4b7ab10b5c38
inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents:
22489
diff
changeset
|
394 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
|
395 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
|
396 this.UsingDefaults = cell (); |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
397 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
|
398 if (this.FunctionName) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
399 print_usage (this.FunctionName); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
400 else |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20575
diff
changeset
|
401 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
|
402 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
403 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
404 pnargin = numel (varargin); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
405 |
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
|
406 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
|
407 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
|
408 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
409 ## Evaluate the Required arguments first |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
410 nReq = numel (this.Required); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
411 for idx = 1:nReq |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
412 req = this.Required{idx}; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
413 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
|
414 endfor |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
415 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
416 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
|
417 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
418 ## 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
|
419 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
|
420 nOpt = numel (this.Optional); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
421 while (vidx < pnargin && idx < nOpt) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
422 opt = this.Optional{++idx}; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
423 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
|
424 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
|
425 || 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
|
426 ## 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
|
427 ## 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
|
428 ## 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
|
429 ## keys. See bug #50752. |
23365
7b594fcfa32b
improve inputParser compatibility for optional args (bug #50752)
John W. Eaton <jwe@octave.org>
parents:
23337
diff
changeset
|
430 idx -= 1; |
7b594fcfa32b
improve inputParser compatibility for optional args (bug #50752)
John W. Eaton <jwe@octave.org>
parents:
23337
diff
changeset
|
431 vidx -= 1; |
28917
72d57dbcc305
maint: Add semicolon after break and return keywords.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
432 break; |
23365
7b594fcfa32b
improve inputParser compatibility for optional args (bug #50752)
John W. Eaton <jwe@octave.org>
parents:
23337
diff
changeset
|
433 endif |
21666
09517e0a8aa0
inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents:
21634
diff
changeset
|
434 try |
09517e0a8aa0
inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents:
21634
diff
changeset
|
435 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
|
436 catch |
09517e0a8aa0
inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents:
21634
diff
changeset
|
437 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
|
438 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
|
439 if (! valid_option) |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
440 ## 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
|
441 ## 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
|
442 ## 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
|
443 ## 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
|
444 ## 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
|
445 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
|
446 && isscalar (in))) |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20726
diff
changeset
|
447 idx -= 1; |
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20726
diff
changeset
|
448 vidx -= 1; |
28917
72d57dbcc305
maint: Add semicolon after break and return keywords.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
449 break; |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
450 else |
23165
5291b67ff124
More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents:
23164
diff
changeset
|
451 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
|
452 "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
|
453 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
|
454 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
455 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
456 this.Results.(opt.name) = in; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
457 endwhile |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
458 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
459 ## 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
|
460 while (idx++ < nOpt) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
461 opt = this.Optional{idx}; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
462 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
|
463 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
|
464 endwhile |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
465 |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
466 ## 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
|
467 while (vidx++ < pnargin) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
468 name = varargin{vidx}; |
21667
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
469 |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
470 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
|
471 expanded_options = [fieldnames(name) struct2cell(name)]'(:); |
25628
a0d642f24349
Parse empty struct input correctly (bug #54318).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
472 if (isempty (expanded_options)) |
a0d642f24349
Parse empty struct input correctly (bug #54318).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
473 continue; # empty, continue to next argument |
a0d642f24349
Parse empty struct input correctly (bug #54318).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
474 endif |
21667
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
475 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
|
476 pnargin += n_new_args; |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
477 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
|
478 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
|
479 name = varargin{vidx}; |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
480 endif |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
481 |
23383
62320e943696
inputParser.m: check if Param key and Switch are strings for better errors.
Carnë Draug <carandraug@octave.org>
parents:
23382
diff
changeset
|
482 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
|
483 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
|
484 endif |
62320e943696
inputParser.m: check if Param key and Switch are strings for better errors.
Carnë Draug <carandraug@octave.org>
parents:
23382
diff
changeset
|
485 |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
486 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
|
487 if (vidx++ > pnargin) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
488 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
|
489 toupper (name))); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
490 endif |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19597
diff
changeset
|
491 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
|
492 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
|
493 varargin{vidx}); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
494 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
|
495 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
|
496 else |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
497 if (vidx++ < pnargin && this.KeepUnmatched) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
498 this.Unmatched.(name) = varargin{vidx}; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
499 else |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
500 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
|
501 toupper (name))); |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
502 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
503 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
504 endwhile |
23337
f04f32f08590
Override disp rather than display for class objects (bug #50640).
Rik <rik@octave.org>
parents:
23333
diff
changeset
|
505 ## 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
|
506 this.add_missing ("Parameter"); |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
507 this.add_missing ("Switch"); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
508 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
509 endfunction |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
510 |
23337
f04f32f08590
Override disp rather than display for class objects (bug #50640).
Rik <rik@octave.org>
parents:
23333
diff
changeset
|
511 function disp (this) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
512 |
23333
b636f10ce53f
fix display methods to only expect one argument
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
513 if (nargin != 1) |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
514 print_usage (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
515 endif |
23337
f04f32f08590
Override disp rather than display for class objects (bug #50640).
Rik <rik@octave.org>
parents:
23333
diff
changeset
|
516 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
|
517 b2s = @(x) ifelse (any (x), "true", "false"); |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
518 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
|
519 " 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
|
520 " StructExpand : %s\n\n"], |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
521 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
|
522 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
|
523 b2s (this.StructExpand)); |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
524 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
|
525 strjoin (this.Parameters, ", ")); |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
526 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
527 endfunction |
28945
6e460773bdda
maint: Use newlines after "function" and before "endfunction" for clarity.
Rik <rik@octave.org>
parents:
28929
diff
changeset
|
528 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
529 endmethods |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
530 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
531 methods (Access = private) |
28945
6e460773bdda
maint: Use newlines after "function" and before "endfunction" for clarity.
Rik <rik@octave.org>
parents:
28929
diff
changeset
|
532 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
533 function validate_name (this, type, name) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
534 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
535 if (! isvarname (name)) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
536 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
|
537 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
|
538 ## 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
|
539 ## 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
|
540 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
|
541 type, name); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
542 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
543 this.Parameters{end+1} = name; |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
544 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
545 endfunction |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
546 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
547 function validate_arg (this, name, val, in) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
548 |
31074
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
549 ## Checking "nargout (val)" doesn't work for builtin functions. |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
550 ## So, we need to use this nested try-catch construct. |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
551 err = sprintf ('Checked with "%s"', func2str (val)); |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
552 try |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
553 ok = val (in); |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
554 catch exception |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
555 if (strcmp (exception.identifier, "Octave:invalid-fun-call")) |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
556 ## check if function also fails when called without output argument |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
557 try |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
558 val (in); |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
559 ok = true; |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
560 catch exception |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
561 ok = false; |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
562 err = exception.message; |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
563 end_try_catch |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
564 else |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
565 ok = false; |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
566 err = exception.message; |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
567 endif |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
568 end_try_catch |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
569 if (! ok) |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
570 this.error (sprintf ("failed validation of %s. %s", |
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
571 toupper (name), err)); |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
572 endif |
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
573 this.Results.(name) = in; |
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
574 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
575 endfunction |
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 function r = is_argname (this, type, name) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
578 |
28187
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
579 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
|
580 if (r) |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
581 if (this.CaseSensitive) |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
582 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
|
583 if (r) |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
584 this.last_name = name; |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
585 endif |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
586 else |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
587 fnames = this.([type "Names"]); |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
588 l = strcmpi (name, fnames); |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
589 r = any (l(:)); |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
590 if (r) |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
591 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
|
592 endif |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
593 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
594 endif |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
595 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
596 endfunction |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
597 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
598 function add_missing (this, type) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
599 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
600 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
|
601 for namec = unmatched(:)' |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
602 name = namec{1}; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
603 this.UsingDefaults{end+1} = name; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
604 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
|
605 endfor |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
606 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
607 endfunction |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
608 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
609 function error (this, msg) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
610 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
611 where = ""; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
612 if (this.FunctionName) |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
613 where = [this.FunctionName ": "]; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
614 endif |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
615 error ("%s%s", where, msg); |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30353
diff
changeset
|
616 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
617 endfunction |
28945
6e460773bdda
maint: Use newlines after "function" and before "endfunction" for clarity.
Rik <rik@octave.org>
parents:
28929
diff
changeset
|
618 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
619 endmethods |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
620 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
621 endclassdef |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
622 |
28945
6e460773bdda
maint: Use newlines after "function" and before "endfunction" for clarity.
Rik <rik@octave.org>
parents:
28929
diff
changeset
|
623 |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
624 %!function p = create_p () |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
625 %! p = inputParser (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
626 %! p.CaseSensitive = true; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
627 %! 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
|
628 %! 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
|
629 %! 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
|
630 %! p.addSwitch ("verbose"); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
631 %! 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
|
632 %!endfunction |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
633 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
634 ## 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
|
635 %!test |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
636 %! p = create_p (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
637 %! p.parse ("file"); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
638 %! r = p.Results; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
639 %! assert (r.req1, "file"); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
640 %! 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
|
641 %! 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
|
642 %! {"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
|
643 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
644 ## 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
|
645 %!test |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
646 %! p = create_p (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
647 %! 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
|
648 %! r = p.Results; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
649 %! 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
|
650 %! {"file", "foo", 80, true, "circle"}); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
651 |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
652 ## 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
|
653 %!test |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
654 %! p = create_p (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
655 %! p.parse ("file", "line", "circle"); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
656 %! r = p.Results; |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19039
diff
changeset
|
657 %! 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
|
658 %! {"file", "val", 78, false, "circle"}); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
659 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
660 ## check case insensitivity |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
661 %!test |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
662 %! p = create_p (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
663 %! p.CaseSensitive = false; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
664 %! 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
|
665 %! r = p.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", "foo", 80, true, "circle"}); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
668 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
669 ## check KeepUnmatched |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
670 %!test |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
671 %! p = create_p (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
672 %! p.KeepUnmatched = true; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
673 %! 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
|
674 %! 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
|
675 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
676 ## check error when missing required |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
677 %!error <not enough input arguments> |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
678 %! p = create_p (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
679 %! p.parse (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
680 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
681 ## 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
|
682 %!error <failed validation of > |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
683 %! p = create_p (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
684 %! p.parse (50); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
685 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
686 ## 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
|
687 %!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
|
688 %! p = create_p (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
689 %! p.parse ("file", "no-val"); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
690 |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
691 ## 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
|
692 %!error <failed validation of > |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
693 %! p = create_p (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
694 %! 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
|
695 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
696 ## check alternative method (obj, ...) API |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
697 %!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
|
698 %! p2 = inputParser (); |
19035
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
699 %! 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
|
700 %! 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
|
701 %! 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
|
702 %! addSwitch (p2, "verbose"); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
703 %! 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
|
704 %!endfunction |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
705 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
706 ## 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
|
707 %!test |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
708 %! p2 = create_p2 (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
709 %! parse (p2, "file"); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
710 %! r = p2.Results; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
711 %! 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
|
712 %! {"file", "val", 78, false, "tree"}); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
713 %! 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
|
714 |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
715 ## 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
|
716 %!test |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
717 %! p2 = create_p2 (); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
718 %! 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
|
719 %! r = p2.Results; |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
720 %! 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
|
721 %! {"file", "foo", 80, true, "circle"}); |
ff820f92cbb5
inputParser: classdef port of @inputParser from Octave Forge general pkg.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
722 |
22489
93ea313301f9
test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
723 ## 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
|
724 %!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
|
725 %! p = inputParser (); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
726 %! 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
|
727 %! p.parse (); |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21578
diff
changeset
|
728 %! 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
|
729 |
a61f0d6beb71
inputParser: do not perform validation of default values (bug #45837)
Carnë Draug <carandraug@octave.org>
parents:
20470
diff
changeset
|
730 %!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
|
731 %! p = inputParser (); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
732 %! 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
|
733 %! p.parse (); |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21578
diff
changeset
|
734 %! 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
|
735 |
21666
09517e0a8aa0
inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents:
21634
diff
changeset
|
736 ## 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
|
737 ## 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
|
738 %!test |
09517e0a8aa0
inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents:
21634
diff
changeset
|
739 %! p = inputParser (); |
09517e0a8aa0
inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents:
21634
diff
changeset
|
740 %! 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
|
741 %! 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
|
742 %! p.parse ("not_err", "qux"); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
743 %! assert (p.Results.err, "foo"); |
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
744 %! assert (p.Results.not_err, "qux"); |
21666
09517e0a8aa0
inputParser.m: handle errors in Optional parameters (maybe is a param key).
Carnë Draug <carandraug@octave.org>
parents:
21634
diff
changeset
|
745 |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
746 ## 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
|
747 %!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
|
748 %! p3 = inputParser (); |
21667
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
749 %! 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
|
750 %! 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
|
751 %! addSwitch (p3, "verbose"); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
752 %! 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
|
753 %! 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
|
754 %! 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
|
755 %!endfunction |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
756 |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
757 ## Test StructExpand |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
758 %!test |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
759 %! p3 = create_p3 (); |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
760 %! 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
|
761 %! 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
|
762 %! "line", "circle", "color", "green", |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
763 %! "style", "tt")) |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
764 |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
765 %!test |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
766 %! p3 = create_p3 (); |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
767 %! p3.parse (struct ("line", "circle", "color", "green"), "line", "tree"); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
768 %! assert (p3.Results.line, "tree"); |
21667
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
769 %! p3.parse ("line", "tree", struct ("line", "circle", "color", "green")); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
770 %! assert (p3.Results.line, "circle"); |
21667
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
771 |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
772 %!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
|
773 %! p3 = create_p3 (); |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
774 %! p3.KeepUnmatched = true; |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
775 %! p3.parse (struct ("line", "circle", "color", "green", "bar", "baz")); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
776 %! assert (p3.Unmatched.bar, "baz"); |
21667
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
777 |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
778 ## 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
|
779 ## 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
|
780 %!test |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
781 %! p3 = create_p3 (); |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
782 %! p3.parse ("foo", struct ("color", "green"), "line", "tree"); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
783 %! assert (p3.Results.op1, "foo"); |
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
784 %! assert (p3.Results.line, "tree"); |
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
785 %! assert (p3.Results.color, "green"); |
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
786 %! assert (p3.Results.verbose, false); |
21667
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
787 |
84092ccb45e2
inputParser.m: implement StructExpand option (struct with param/value pairs).
Carnë Draug <carandraug@octave.org>
parents:
21666
diff
changeset
|
788 |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
789 ## 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
|
790 ## 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
|
791 %!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
|
792 %! p = inputParser (); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
793 %! 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
|
794 %! 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
|
795 %! p.parse ("line", "circle"); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
796 %! assert ({p.Results.line, p.Results.color}, {"circle", "red"}); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
797 |
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
|
798 %!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
|
799 %! 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
|
800 %! 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
|
801 %! p.parse (); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
802 %! assert (p.Results, struct ("foo", "bar")); |
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
|
803 %! p.parse ("foo", "qux"); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
804 %! 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
|
805 |
23377
f1bf2590272a
inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents:
23365
diff
changeset
|
806 ## 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
|
807 ## 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
|
808 ## compatibility. |
23573
1b4f4ec53b4a
use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents:
23572
diff
changeset
|
809 %!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
|
810 %! p = inputParser (); |
21505
7f7d7cb73e0d
inputParser.m: uncomment failing test and use %!xtest instead.
Carnë Draug <carandraug@octave.org>
parents:
21316
diff
changeset
|
811 %! p.addOptional ("op1", "val"); |
21677
1cd04fb745f6
inputParser: new method addParameter --- just like addParamValue (bug #45367)
Carnë Draug <carandraug@octave.org>
parents:
21668
diff
changeset
|
812 %! p.addParameter ("line", "tree"); |
21505
7f7d7cb73e0d
inputParser.m: uncomment failing test and use %!xtest instead.
Carnë Draug <carandraug@octave.org>
parents:
21316
diff
changeset
|
813 %! p.parse ("line", "circle"); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
814 %! assert (p.Results, struct ("op1", "val", "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
|
815 %! |
f1bf2590272a
inputParser.m: do not confuse Switch options with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents:
23365
diff
changeset
|
816 %! 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
|
817 %! 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
|
818 %! 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
|
819 %! 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
|
820 %! p.parse ("line", "circle"); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
821 %! assert (p.Results.op1, "val1"); |
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
822 %! assert (p.Results.op2, "val2"); |
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
823 %! 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
|
824 %! |
5a3c3ff03167
inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents:
23377
diff
changeset
|
825 %! ## 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
|
826 %! ## 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
|
827 %! 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
|
828 %! 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
|
829 %! 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
|
830 %! 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
|
831 %! 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
|
832 %! 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
|
833 %! "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
|
834 %! |
5a3c3ff03167
inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents:
23377
diff
changeset
|
835 %! ## 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
|
836 %! ## 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
|
837 %! 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
|
838 %! 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
|
839 %! 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
|
840 %! 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
|
841 %! 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
|
842 %! |
5a3c3ff03167
inputParser.m: more tests for mix Param keys with positional options (bug #50752)
Carnë Draug <carandraug@octave.org>
parents:
23377
diff
changeset
|
843 %! 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
|
844 %! 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
|
845 %! 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
|
846 %! 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
|
847 %! "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
|
848 |
23573
1b4f4ec53b4a
use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents:
23572
diff
changeset
|
849 %!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
|
850 %! ## 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
|
851 %! ## 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
|
852 %! 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
|
853 %! 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
|
854 %! 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
|
855 %! p.parse ("line"); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
856 %! 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
|
857 |
23573
1b4f4ec53b4a
use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents:
23572
diff
changeset
|
858 %!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
|
859 %! 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
|
860 %! 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
|
861 %! 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
|
862 %! p.parse ("line"); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
863 %! assert (p.Results.op1, "val1"); |
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28917
diff
changeset
|
864 %! 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
|
865 |
4b7ab10b5c38
inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents:
22489
diff
changeset
|
866 %!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
|
867 %! p = inputParser (); |
22541
4b7ab10b5c38
inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents:
22489
diff
changeset
|
868 %! p.addParameter ("a", []); |
4b7ab10b5c38
inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents:
22489
diff
changeset
|
869 %! p.addParameter ("b", []); |
4b7ab10b5c38
inputParser clear previous results before new parse (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents:
22489
diff
changeset
|
870 %! 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
|
871 %! 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
|
872 %! 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
|
873 %! 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
|
874 |
22559
27f2a7a3b55d
inputParser reset more parse results before new parse. (bug #49198)
Carlo de Falco <carlo.defalco@polimi.it>
parents:
22541
diff
changeset
|
875 %!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
|
876 %! 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
|
877 %! 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
|
878 %! 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
|
879 %! 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
|
880 %! 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
|
881 %! 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
|
882 %! assert (p.Unmatched, struct ()); |
23165
5291b67ff124
More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents:
23164
diff
changeset
|
883 |
5291b67ff124
More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents:
23164
diff
changeset
|
884 ## Test for patch #9241 |
31074
2c8ab613e805
inputParser.m: Adapt for interpreter changes regarding number of output arguments (bug #62420).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
885 %!error <failed validation of A.*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
|
886 %! p = inputParser (); |
23165
5291b67ff124
More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents:
23164
diff
changeset
|
887 %! p.addParameter ("a", [], @ischar); |
5291b67ff124
More verbose information when inputParser fails validating argument (patch #9241)
Georg Wiora
parents:
23164
diff
changeset
|
888 %! 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
|
889 |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
890 %!test <*58112> |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
891 %! p = inputParser (); |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
892 %! p.addRequired ("first"); |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
893 %! p.addOptional ("second", []); |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
894 %! 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
|
895 %! r = p.Results; |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
896 %! assert (r.first, 1); |
3faa700d64d4
inputParser.m: Fix error with cell array Optional argument (bug #58112)
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
897 %! assert (r.second, {"test", 1, 2, 3}); |
31103
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
898 |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
899 %!test <*62639> |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
900 %! p = inputParser (); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
901 %! p.addOptional ("opt", {}); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
902 %! p.parse (); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
903 %! r = p.Results; |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
904 %! assert (r.opt, {}); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
905 %! p.parse ("x"); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
906 %! r = p.Results; |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
907 %! assert (r.opt, "x"); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
908 |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
909 %!test <*62639> |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
910 %! p = inputParser (); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
911 %! p.addOptional ("opt", {1,2,3}); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
912 %! p.parse (); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
913 %! r = p.Results; |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
914 %! assert (r.opt, {1,2,3}); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
915 %! p.parse ("x"); |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
916 %! r = p.Results; |
7797481038fc
inputParser: Add some tests for bug #62639.
Markus Mützel <markus.muetzel@gmx.de>
parents:
31101
diff
changeset
|
917 %! assert (r.opt, "x"); |