Mercurial > octave-nkf
annotate scripts/miscellaneous/parseparams.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 94e2a76f1e5a |
children | 7277fe922e99 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14119
diff
changeset
|
1 ## Copyright (C) 2006-2012 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 -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10308
diff
changeset
|
21 ## @deftypefn {Function File} {[@var{reg}, @var{prop}] =} parseparams (@var{params}) |
10308 | 22 ## @deftypefnx {Function File} {[@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 |
24 ## string element and in @var{prop} all remaining elements beginning | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
25 ## with the first string element. For example: |
5981 | 26 ## |
27 ## @example | |
5996 | 28 ## @group |
5981 | 29 ## [reg, prop] = parseparams (@{1, 2, "linewidth", 10@}) |
30 ## reg = | |
5996 | 31 ## @{ |
5981 | 32 ## [1,1] = 1 |
33 ## [1,2] = 2 | |
5996 | 34 ## @} |
5981 | 35 ## prop = |
5996 | 36 ## @{ |
5981 | 37 ## [1,1] = linewidth |
38 ## [1,2] = 10 | |
5996 | 39 ## @} |
40 ## @end group | |
5981 | 41 ## @end example |
42 ## | |
5996 | 43 ## The parseparams function may be used to separate 'regular' |
5981 | 44 ## arguments and additional arguments given as property/value pairs of |
45 ## the @var{varargin} cell array. | |
10308 | 46 ## |
47 ## In the second form of the call, available options are specified directly | |
48 ## with their default values given as name-value pairs. | |
49 ## If @var{params} do not form name-value pairs, or if an option occurs | |
50 ## that does not match any of the available options, an error occurs. | |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
51 ## When called from an m-file function, the error is prefixed with the |
10308 | 52 ## name of the caller function. |
53 ## The matching of options is case-insensitive. | |
54 ## | |
5981 | 55 ## @seealso{varargin} |
56 ## @end deftypefn | |
57 | |
58 ## Author: Alexander Barth <abarth93@users.sourceforge.net> | |
59 ## Author: Aida Alvera Azcarate <aida@netecho.info> | |
60 | |
10308 | 61 function [reg, varargout] = parseparams (params, varargin) |
5981 | 62 |
10308 | 63 strs = cellfun ("isclass", params, "char"); |
64 i = find (strs, 1); | |
65 if (i) | |
66 reg = params(1:i-1); | |
67 prop = params(i:end); | |
68 else | |
69 reg = params; | |
70 prop = {}; | |
71 endif | |
5981 | 72 |
10308 | 73 if (nargin == 1) |
74 varargout = {prop}; | |
75 else | |
76 names = varargin(1:2:end); | |
77 defaults = varargin(2:2:end); | |
78 if (! size_equal (names, defaults)) | |
79 error ("parseparams: needs odd number of arguments"); | |
5981 | 80 endif |
10308 | 81 [names, sidx] = sort (names); |
5981 | 82 |
10308 | 83 varargout = defaults; |
84 if (i) | |
85 ## Let's parse the properties. | |
86 pnames = prop(1:2:end); | |
87 values = prop(2:2:end); | |
88 if (! size_equal (pnames, values) || ! all (strs(i:2:end))) | |
89 error_as_caller ("options must be given as name-value pairs"); | |
90 endif | |
11372
566249cf97fb
Fix typo introduced when removing 'i' support in lookup.
Rik <octave@nomad.inbox5.com>
parents:
11346
diff
changeset
|
91 idx = lookup (toupper(names), toupper(pnames), "m"); |
10308 | 92 if (! all (idx)) |
93 error_as_caller ("unrecognized option: %s", pnames{find (idx == 0, 1)}); | |
94 else | |
95 varargout(sidx(idx)) = values; | |
96 endif | |
97 endif | |
98 endif | |
5981 | 99 |
100 endfunction | |
10308 | 101 |
102 function error_as_caller (msg, varargin) | |
103 stack = dbstack (1); # omit me | |
104 fname = stack(min (2, end)).name; | |
105 error ([fname, ": ", msg], varargin{:}); | |
106 endfunction | |
107 |