-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetOptions.m
executable file
·187 lines (161 loc) · 4.39 KB
/
GetOptions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
function options = GetOptions(defaults, args, ignoreExtra)
% options = GetOptions(defaults, args)
% Create a structure of options from defaults and user-overrides.
% INPUTS:
% -defaults: a cell array with keyword, value ordering
% or a structure
% -args: either a structure or
% a cell array
% if a cell array, user can pass ALL keywords or
% just go by input order.
% if args is ommitted or length zero, the options will be the defaults.
% OPTIONAL
% -ignoreExtra false
% if set to true, ignore options specified in args that don't correspond
% to anything in defaults.
% OUTPUTS:
% -options: structure with fields set to their proper values
if nargin < 1 || nargin > 3
help GetOptions
error('Invalid number of inputs.')
end
if ~iscell(defaults) && ~isstruct(defaults)
help GetOptions
error('"defaults" must be a cell array or structure.')
end
if iscell(defaults);
try
defaults = cellOptionsToStruct(defaults);
catch err
% get help for the m-file that called GetOptions
getHelp()
% rethrow error
rethrow(err)
end
end
if nargin == 1 || isempty(args)
options = defaults;
return
end
if nargin < 3
ignoreExtra = false;
end
if iscell(args) && length(args) == 1 && isstruct(args{1})
args = args{1};
end
if isstruct(args)
% user passed a struct containing options
try
options = getStructOptions(defaults, args, ignoreExtra);
catch err
% get help for the m-file that called GetOptions
getHelp()
% rethrow error
rethrow(err)
end
elseif iscell(args)
% user passed a cell array containing options
try
options = getCellArrOptions(defaults, args, ignoreExtra);
catch err
% get help for the m-file that called GetOptions
getHelp()
% rethrow error
rethrow(err)
end
else
help GetOptions
error('"args" must be a structure or a cell array.')
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function optStruct = cellOptionsToStruct(cellOpts)
numOpts = length(cellOpts);
if mod(numOpts, 2) ~= 0
error('options must come in "key" "value" pairs.')
end
numOpts = numOpts / 2;
for n = 1:numOpts
mVal = 2 * n;
mKey = mVal - 1;
key = cellOpts{mKey};
val = cellOpts{mVal};
if ~ischar(key)
error('Invalid option key: %s', key)
end
optStruct.(key) = val;
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function options = getStructOptions(defaults, argStruct, ignoreExtra)
% get options as defaults with overrides specified by argStruct
options = defaults;
fNames = fieldnames(argStruct);
for n = 1:length(fNames)
field_n = fNames{n};
value_n = argStruct.(field_n);
if isempty(value_n)
% skip empty values
continue
end
if isfield(options, field_n)
% this is a valid field, override the default
options.(field_n) = value_n;
elseif ~ignoreExtra
error('Invalid option: %s', field_n)
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function options = getCellArrOptions(defaults, args, ignoreExtra)
% get options as default with overrides specified by cell array args
fNames = fieldnames(defaults);
keys = args(1:2:end);
options = defaults;
if mod(length(args), 2) == 0 && ~isempty(intersect(fNames, keys))
% overrides are keyword, value pairs
if length(keys) > length(fNames)
error('Too many arguments')
end
values = args(2:2:end);
for n = 1:length(keys)
if isempty(values{n})
% skip empty values
continue
end
if isfield(defaults, keys{n})
% this is a valid field, override the default
options.(keys{n}) = values{n};
elseif ~ignoreExtra
error('Invalid option: %s', keys{n});
end
end
else
% overrides are just a list of values
if length(args) > length(fNames)
error('Too many arguments')
end
for n = 1:length(fNames)
options.(fNames{n}) = args{n};
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function getHelp()
% get help for the m-file that called GetOptions
st = dbstack;
thisFile = mfilename;
for n = 1:length(st)
if ~strcmp(st(n).file, thisFile)
%this is the calling file
callFile = st(n).file;
ind = find(callFile == filesep, 1, 'last');
if ~isempty(ind)
callFile = callFile((ind+1):end);
end
help(callFile)
return
end
end
help GetOptions
return