|
| 1 | +function [opts, args] = fix_vl_argparse(opts, args) |
| 2 | +% VL_ARGPARSE Parse list of parameter-value pairs |
| 3 | +% OPTS = VL_ARGPARSE(OPTS, ARGS) updates the structure OPTS based on |
| 4 | +% the specified parameter-value pairs ARGS={PAR1, VAL1, ... PARN, |
| 5 | +% VALN}. The function produces an error if an unknown parameter name |
| 6 | +% is passed on. Values that are structures are copied recursively. |
| 7 | +% |
| 8 | +% Any of the PAR, VAL pairs can be replaced by a structure; in this |
| 9 | +% case, the fields of the structure are used as paramaters and the |
| 10 | +% field values as values. |
| 11 | +% |
| 12 | +% [OPTS, ARGS] = VL_ARGPARSE(OPTS, ARGS) copies any parameter in |
| 13 | +% ARGS that does not match OPTS back to ARGS instead of producing an |
| 14 | +% error. Options specified as structures are expaned back to PAR, |
| 15 | +% VAL pairs. |
| 16 | +% |
| 17 | +% Example:: |
| 18 | +% The function can be used to parse a list of arguments |
| 19 | +% passed to a MATLAB functions: |
| 20 | +% |
| 21 | +% function myFunction(x,y,z,varargin) |
| 22 | +% opts.parameterName = defaultValue ; |
| 23 | +% opts = vl_argparse(opts, varargin) |
| 24 | +% |
| 25 | +% If only a subset of the options should be parsed, for example |
| 26 | +% because the other options are interpreted by a subroutine, then |
| 27 | +% use the form |
| 28 | +% |
| 29 | +% [opts, varargin] = vl_argparse(opts, varargin) |
| 30 | +% |
| 31 | +% that copies back to VARARGIN any unknown parameter. |
| 32 | +% |
| 33 | +% See also: VL_HELP(). |
| 34 | + |
| 35 | +% Authors: Andrea Vedaldi |
| 36 | + |
| 37 | +% Copyright (C) 2015 Andrea Vedaldi. |
| 38 | +% Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson. |
| 39 | +% All rights reserved. |
| 40 | +% |
| 41 | +% This file is part of the VLFeat library and is made available under |
| 42 | +% the terms of the BSD license (see the COPYING file). |
| 43 | + |
| 44 | +if ~isstruct(opts), error('OPTS must be a structure') ; end |
| 45 | +if ~iscell(args), args = {args} ; end |
| 46 | + |
| 47 | +% convert ARGS into a structure |
| 48 | +ai = 1 ; |
| 49 | +params = {} ; |
| 50 | +values = {} ; |
| 51 | +while ai <= length(args) |
| 52 | + if isstr(args{ai}) |
| 53 | + params{end+1} = args{ai} ; ai = ai + 1 ; |
| 54 | + values{end+1} = args{ai} ; ai = ai + 1 ; |
| 55 | + elseif isstruct(args{ai}) ; |
| 56 | + params = horzcat(params, fieldnames(args{ai})') ; |
| 57 | + values = horzcat(values, struct2cell(args{ai})') ; |
| 58 | + ai = ai + 1 ; |
| 59 | + else |
| 60 | + error('Expected either a param-value pair or a structure') ; |
| 61 | + end |
| 62 | +end |
| 63 | +args = {} ; |
| 64 | + |
| 65 | +% copy parameters in the opts structure, recursively |
| 66 | +for i = 1:numel(params) |
| 67 | + field = findfield(opts, params{i}) ; |
| 68 | + if ~isempty(field) |
| 69 | + if isstruct(values{i}) |
| 70 | + if ~isstruct(opts.(field)) |
| 71 | + error('The value of parameter %d is a structure in the arguments but not a structure in OPT.',field) ; |
| 72 | + end |
| 73 | + if nargout > 1 |
| 74 | + [opts.(field), rest] = fix_vl_argparse(opts.(field), values{i}) ; |
| 75 | + args = horzcat(args, {field, cell2struct(rest(2:2:end), rest(1:2:end), 2)}) ; |
| 76 | + else |
| 77 | + opts.(field) = values{i} ; |
| 78 | + end |
| 79 | + else |
| 80 | + opts.(field) = values{i} ; |
| 81 | + end |
| 82 | + else |
| 83 | + if nargout <= 1 |
| 84 | + error('Uknown parameter ''%s''', params{i}) ; |
| 85 | + else |
| 86 | + args = horzcat(args, {params{i}, values{i}}) ; |
| 87 | + end |
| 88 | + end |
| 89 | +end |
| 90 | + |
| 91 | +function field = findfield(opts, field) |
| 92 | +fields=fieldnames(opts) ; |
| 93 | +i=find(strcmpi(fields, field)) ; |
| 94 | +if ~isempty(i) |
| 95 | + field=fields{i} ; |
| 96 | +else |
| 97 | + field=[] ; |
| 98 | +end |
| 99 | + |
0 commit comments