Mercurial > hg > octave-jordi
annotate scripts/miscellaneous/parseparams.m @ 21052:b702efa70fb5 draft default tip shane
Adding --json-sock option; publishes octave_link events to a UNIX socket.
author | Shane F. Carr <shane.carr@wustl.edu> |
---|---|
date | Mon, 04 Jan 2016 08:51:31 +0000 |
parents | 516bb87ea72e |
children |
rev | line source |
---|---|
19696
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19178
diff
changeset
|
1 ## Copyright (C) 2006-2015 Alexander Barth |
10308 | 2 ## Copyright (C) 2010 VZLU Prague |
5981 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5981 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5981 | 19 |
20 ## -*- texinfo -*- | |
20851
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20163
diff
changeset
|
21 ## @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:
20163
diff
changeset
|
22 ## @deftypefnx {} {[@var{reg}, @var{var1}, @dots{}] =} parseparams (@var{params}, @var{name1}, @var{default1}, @dots{}) |
5981 | 23 ## Return in @var{reg} the cell elements of @var{param} up to the first |
20163
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19696
diff
changeset
|
24 ## 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:
19696
diff
changeset
|
25 ## first string element. |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19696
diff
changeset
|
26 ## |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19696
diff
changeset
|
27 ## For example: |
5981 | 28 ## |
29 ## @example | |
5996 | 30 ## @group |
5981 | 31 ## [reg, prop] = parseparams (@{1, 2, "linewidth", 10@}) |
32 ## reg = | |
5996 | 33 ## @{ |
5981 | 34 ## [1,1] = 1 |
35 ## [1,2] = 2 | |
5996 | 36 ## @} |
5981 | 37 ## prop = |
5996 | 38 ## @{ |
5981 | 39 ## [1,1] = linewidth |
40 ## [1,2] = 10 | |
5996 | 41 ## @} |
42 ## @end group | |
5981 | 43 ## @end example |
44 ## | |
20163
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19696
diff
changeset
|
45 ## 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:
19696
diff
changeset
|
46 ## 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:
19696
diff
changeset
|
47 ## @var{varargin} cell array. |
10308 | 48 ## |
49 ## In the second form of the call, available options are specified directly | |
20163
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19696
diff
changeset
|
50 ## 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:
19696
diff
changeset
|
51 ## 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:
19696
diff
changeset
|
52 ## 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:
19696
diff
changeset
|
53 ## |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19696
diff
changeset
|
54 ## 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:
19696
diff
changeset
|
55 ## of the caller function. |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19696
diff
changeset
|
56 ## |
10308 | 57 ## The matching of options is case-insensitive. |
58 ## | |
19178
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
59 ## @seealso{varargin, inputParser} |
5981 | 60 ## @end deftypefn |
61 | |
62 ## Author: Alexander Barth <abarth93@users.sourceforge.net> | |
63 ## Author: Aida Alvera Azcarate <aida@netecho.info> | |
64 | |
10308 | 65 function [reg, varargout] = parseparams (params, varargin) |
5981 | 66 |
10308 | 67 strs = cellfun ("isclass", params, "char"); |
68 i = find (strs, 1); | |
69 if (i) | |
70 reg = params(1:i-1); | |
71 prop = params(i:end); | |
72 else | |
73 reg = params; | |
74 prop = {}; | |
75 endif | |
5981 | 76 |
10308 | 77 if (nargin == 1) |
78 varargout = {prop}; | |
79 else | |
80 names = varargin(1:2:end); | |
81 defaults = varargin(2:2:end); | |
82 if (! size_equal (names, defaults)) | |
83 error ("parseparams: needs odd number of arguments"); | |
5981 | 84 endif |
10308 | 85 [names, sidx] = sort (names); |
5981 | 86 |
10308 | 87 varargout = defaults; |
88 if (i) | |
89 ## Let's parse the properties. | |
90 pnames = prop(1:2:end); | |
91 values = prop(2:2:end); | |
92 if (! size_equal (pnames, values) || ! all (strs(i:2:end))) | |
93 error_as_caller ("options must be given as name-value pairs"); | |
94 endif | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
95 idx = lookup (toupper (names), toupper (pnames), "m"); |
10308 | 96 if (! all (idx)) |
97 error_as_caller ("unrecognized option: %s", pnames{find (idx == 0, 1)}); | |
98 else | |
99 varargout(sidx(idx)) = values; | |
100 endif | |
101 endif | |
102 endif | |
5981 | 103 |
104 endfunction | |
10308 | 105 |
106 function error_as_caller (msg, varargin) | |
107 stack = dbstack (1); # omit me | |
108 fname = stack(min (2, end)).name; | |
109 error ([fname, ": ", msg], varargin{:}); | |
110 endfunction | |
111 | |
19178
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
112 |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
113 %!test |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 %! [reg, prop] = parseparams ({1, 2, "linewidth", 10}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
115 %! assert (reg, {[1], [2]}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
116 %! assert (prop, {"linewidth", 10}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
117 %!test |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
118 %! [reg, prop] = parseparams ({1, 2, 3}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
119 %! assert (reg, {[1], [2], [3]}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
120 %! assert (isempty (prop)); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
121 %!test |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
122 %! [reg, prop] = parseparams ({"prop1", "val1"}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
123 %! assert (isempty (reg)); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
124 %! assert (prop, {"prop1", "val1"}); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
125 %!test |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
126 %! [reg, prop1] = parseparams ({"linewidth", 5}, "linewidth", 10); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
127 %! assert (isempty (reg)); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
128 %! assert (prop1, 5); |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
129 |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
130 %!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
|
131 %!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
|
132 %!error <unrecognized option: color> parseparams ({1, "color", 5}, "linewidth", 5) |
751d4a76c221
parseparams.m: Add BIST tests to function.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
133 |