Mercurial > hg > simscene-py
comparison nonExposed/utils/process_options.m @ 14:b1901e8d8f5f
initial commit
author | Mathieu Lagrange <mathieu.lagrange@cnrs.fr> |
---|---|
date | Tue, 17 Mar 2015 09:34:13 +0100 |
parents | |
children | 8ce78cacb5cb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 14:b1901e8d8f5f |
---|---|
1 % PROCESS_OPTIONS - Processes options passed to a Matlab function. | |
2 % This function provides a simple means of | |
3 % parsing attribute-value options. Each option is | |
4 % named by a unique string and is given a default | |
5 % value. | |
6 % | |
7 % Usage: [var1, var2, ..., varn[, unused]] = ... | |
8 % process_options(args, ... | |
9 % str1, def1, str2, def2, ..., strn, defn) | |
10 % | |
11 % Arguments: | |
12 % args - a cell array of input arguments, such | |
13 % as that provided by VARARGIN. Its contents | |
14 % should alternate between strings and | |
15 % values. | |
16 % str1, ..., strn - Strings that are associated with a | |
17 % particular variable | |
18 % def1, ..., defn - Default values returned if no option | |
19 % is supplied | |
20 % NOTE: this version (modified by dpwe) will run str2num on | |
21 % string arguments if the default value is numeric | |
22 % (to support passing in numbers from the command line). | |
23 % | |
24 % Returns: | |
25 % var1, ..., varn - values to be assigned to variables | |
26 % unused - an optional cell array of those | |
27 % string-value pairs that were unused; | |
28 % if this is not supplied, then a | |
29 % warning will be issued for each | |
30 % option in args that lacked a match. | |
31 % | |
32 % Examples: | |
33 % | |
34 % Suppose we wish to define a Matlab function 'func' that has | |
35 % required parameters x and y, and optional arguments 'u' and 'v'. | |
36 % With the definition | |
37 % | |
38 % function y = func(x, y, varargin) | |
39 % | |
40 % [u, v] = process_options(varargin, 'u', 0, 'v', 1); | |
41 % | |
42 % calling func(0, 1, 'v', 2) will assign 0 to x, 1 to y, 0 to u, and 2 | |
43 % to v. The parameter names are insensitive to case; calling | |
44 % func(0, 1, 'V', 2) has the same effect. The function call | |
45 % | |
46 % func(0, 1, 'u', 5, 'z', 2); | |
47 % | |
48 % will result in u having the value 5 and v having value 1, but | |
49 % will issue a warning that the 'z' option has not been used. On | |
50 % the other hand, if func is defined as | |
51 % | |
52 % function y = func(x, y, varargin) | |
53 % | |
54 % [u, v, unused_args] = process_options(varargin, 'u', 0, 'v', 1); | |
55 % | |
56 % then the call func(0, 1, 'u', 5, 'z', 2) will yield no warning, | |
57 % and unused_args will have the value {'z', 2}. This behaviour is | |
58 % useful for functions with options that invoke other functions | |
59 % with options; all options can be passed to the outer function and | |
60 % its unprocessed arguments can be passed to the inner function. | |
61 | |
62 % Copyright (C) 2002 Mark A. Paskin | |
63 % | |
64 % This program is free software; you can redistribute it and/or modify | |
65 % it under the terms of the GNU General Public License as published by | |
66 % the Free Software Foundation; either version 2 of the License, or | |
67 % (at your option) any later version. | |
68 % | |
69 % This program is distributed in the hope that it will be useful, but | |
70 % WITHOUT ANY WARRANTY; without even the implied warranty of | |
71 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
72 % General Public License for more details. | |
73 % | |
74 % You should have received a copy of the GNU General Public License | |
75 % along with this program; if not, write to the Free Software | |
76 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
77 % USA. | |
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
79 | |
80 function [varargout] = process_options(args, varargin) | |
81 | |
82 % Check the number of input arguments | |
83 n = length(varargin); | |
84 if (mod(n, 2)) | |
85 error('Each option must be a string/value pair.'); | |
86 end | |
87 | |
88 % Check the number of supplied output arguments | |
89 if (nargout < (n / 2)) | |
90 error('Insufficient number of output arguments given'); | |
91 elseif (nargout == (n / 2)) | |
92 warn = 1; | |
93 nout = n / 2; | |
94 else | |
95 warn = 0; | |
96 nout = n / 2 + 1; | |
97 end | |
98 | |
99 % Set outputs to be defaults | |
100 varargout = cell(1, nout); | |
101 for i=2:2:n | |
102 varargout{i/2} = varargin{i}; | |
103 end | |
104 | |
105 % Now process all arguments | |
106 nunused = 0; | |
107 for i=1:2:length(args) | |
108 found = 0; | |
109 for j=1:2:n | |
110 if strcmpi(args{i}, varargin{j}) | |
111 % dpwe: promote string args to numeric if default arg is | |
112 % numeric | |
113 if isnumeric(varargin{j+1}) && ischar(args{i+1}) | |
114 varargout{(j + 1)/2} = str2num(args{i + 1}); | |
115 % this occurs when specifying arguments from command line | |
116 else | |
117 % in all other cases, take what you get | |
118 varargout{(j + 1)/2} = args{i + 1}; | |
119 end | |
120 found = 1; | |
121 break; | |
122 end | |
123 end | |
124 if (~found) | |
125 if (warn) | |
126 warning(sprintf('Option ''%s'' not used.', args{i})); | |
127 args{i} | |
128 else | |
129 nunused = nunused + 1; | |
130 unused{2 * nunused - 1} = args{i}; | |
131 if length(args) > i | |
132 % don't demand a value for the last, unused tag (e.g. -help) | |
133 unused{2 * nunused} = args{i + 1}; | |
134 end | |
135 end | |
136 end | |
137 end | |
138 | |
139 % Assign the unused arguments | |
140 if (~warn) | |
141 if (nunused) | |
142 varargout{nout} = unused; | |
143 else | |
144 varargout{nout} = cell(0); | |
145 end | |
146 end |