Mercurial > octave
annotate scripts/miscellaneous/parseparams.m @ 29358:0a5b15007766 stable
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2021.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 10 Feb 2021 09:52:15 -0500 |
parents | da4017484f19 |
children | 01de0045b2e3 |
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 ## |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
28423
diff
changeset
|
3 ## Copyright (C) 2006-2021 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
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/>. |
5981 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
5981 | 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:
23220
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:
22323
diff
changeset
|
13 ## (at your option) any later version. |
5981 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
5981 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 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:
23220
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 ######################################################################## |
5981 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
27 ## @deftypefn {} {[@var{reg}, @var{prop}] =} parseparams (@var{params}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
28 ## @deftypefnx {} {[@var{reg}, @var{var1}, @dots{}] =} parseparams (@var{params}, @var{name1}, @var{default1}, @dots{}) |
5981 | 29 ## Return in @var{reg} the cell elements of @var{param} up to the first |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
30 ## string element and in @var{prop} all remaining elements beginning with the |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
31 ## first string element. |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
32 ## |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
33 ## For example: |
5981 | 34 ## |
35 ## @example | |
5996 | 36 ## @group |
5981 | 37 ## [reg, prop] = parseparams (@{1, 2, "linewidth", 10@}) |
38 ## reg = | |
5996 | 39 ## @{ |
5981 | 40 ## [1,1] = 1 |
41 ## [1,2] = 2 | |
5996 | 42 ## @} |
5981 | 43 ## prop = |
5996 | 44 ## @{ |
5981 | 45 ## [1,1] = linewidth |
46 ## [1,2] = 10 | |
5996 | 47 ## @} |
48 ## @end group | |
5981 | 49 ## @end example |
50 ## | |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
51 ## The parseparams function may be used to separate regular numeric arguments |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
52 ## from additional arguments given as property/value pairs of the |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
53 ## @var{varargin} cell array. |
10308 | 54 ## |
55 ## In the second form of the call, available options are specified directly | |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
56 ## with their default values given as name-value pairs. If @var{params} do |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
57 ## not form name-value pairs, or if an option occurs that does not match any |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
58 ## of the available options, an error occurs. |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
59 ## |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
60 ## When called from an m-file function, the error is prefixed with the name |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
61 ## of the caller function. |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
62 ## |
10308 | 63 ## The matching of options is case-insensitive. |
64 ## | |
19179
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
65 ## @seealso{varargin, inputParser} |
5981 | 66 ## @end deftypefn |
67 | |
10308 | 68 function [reg, varargout] = parseparams (params, varargin) |
5981 | 69 |
10308 | 70 strs = cellfun ("isclass", params, "char"); |
71 i = find (strs, 1); | |
72 if (i) | |
73 reg = params(1:i-1); | |
74 prop = params(i:end); | |
75 else | |
76 reg = params; | |
77 prop = {}; | |
78 endif | |
5981 | 79 |
10308 | 80 if (nargin == 1) |
81 varargout = {prop}; | |
82 else | |
83 names = varargin(1:2:end); | |
84 defaults = varargin(2:2:end); | |
85 if (! size_equal (names, defaults)) | |
86 error ("parseparams: needs odd number of arguments"); | |
5981 | 87 endif |
28423
da4017484f19
parseparams.m: Fix case sensitivity bug with default inputs (bug #58533).
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
88 [names, sidx] = sort (toupper (names)); |
5981 | 89 |
10308 | 90 varargout = defaults; |
91 if (i) | |
92 ## Let's parse the properties. | |
93 pnames = prop(1:2:end); | |
94 values = prop(2:2:end); | |
95 if (! size_equal (pnames, values) || ! all (strs(i:2:end))) | |
96 error_as_caller ("options must be given as name-value pairs"); | |
97 endif | |
28423
da4017484f19
parseparams.m: Fix case sensitivity bug with default inputs (bug #58533).
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
98 idx = lookup (names, toupper (pnames), "m"); |
10308 | 99 if (! all (idx)) |
100 error_as_caller ("unrecognized option: %s", pnames{find (idx == 0, 1)}); | |
101 else | |
102 varargout(sidx(idx)) = values; | |
103 endif | |
104 endif | |
105 endif | |
5981 | 106 |
107 endfunction | |
10308 | 108 |
109 function error_as_caller (msg, varargin) | |
110 stack = dbstack (1); # omit me | |
111 fname = stack(min (2, end)).name; | |
112 error ([fname, ": ", msg], varargin{:}); | |
113 endfunction | |
114 | |
19179
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
115 |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
116 %!test |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
117 %! [reg, prop] = parseparams ({1, 2, "linewidth", 10}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
118 %! assert (reg, {[1], [2]}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
119 %! assert (prop, {"linewidth", 10}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
120 %!test |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
121 %! [reg, prop] = parseparams ({1, 2, 3}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
122 %! assert (reg, {[1], [2], [3]}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
123 %! assert (isempty (prop)); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
124 %!test |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
125 %! [reg, prop] = parseparams ({"prop1", "val1"}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
126 %! assert (isempty (reg)); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
127 %! assert (prop, {"prop1", "val1"}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
128 %!test |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
129 %! [reg, prop1] = parseparams ({"linewidth", 5}, "linewidth", 10); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
130 %! assert (isempty (reg)); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
131 %! assert (prop1, 5); |
28423
da4017484f19
parseparams.m: Fix case sensitivity bug with default inputs (bug #58533).
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
132 %!test <*58533> |
da4017484f19
parseparams.m: Fix case sensitivity bug with default inputs (bug #58533).
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
133 %! [foo, bar1, bar2] = parseparams({ 0, "model", 1, "N", 2},"model", 4, "N", 5, "d", 6); |
da4017484f19
parseparams.m: Fix case sensitivity bug with default inputs (bug #58533).
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
134 %! assert (foo, {0}); |
da4017484f19
parseparams.m: Fix case sensitivity bug with default inputs (bug #58533).
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
135 %! assert (bar1, 1); |
da4017484f19
parseparams.m: Fix case sensitivity bug with default inputs (bug #58533).
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
136 %! assert (bar2, 2); |
19179
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
137 |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
138 %!error <needs odd number of arguments> parseparams ({1}, "linewidth") |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
139 %!error <must be given as name-value pairs> parseparams ({1, "color"}, "linewidth", 5) |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
140 %!error <unrecognized option: color> parseparams ({1, "color", 5}, "linewidth", 5) |