-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmp_argparse.m
85 lines (74 loc) · 2.2 KB
/
cmp_argparse.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
function [conf, args] = cmp_argparse(conf, args, varargin)
% VL_ARGPARSE Parse list of parameter-value pairs
% CONF = VL_ARGPARSE(CONF, ARGS) updates the structure CONF based on
% the specified parameter-value pairs ARGS={PAR1, VAL1, ... PARN,
% VALN}. The function produces an error if an unknown parameter name
% is passed in.
%
% [CONF, ARGS] = VL_ARGPARSE(CONF, ARGS) copies any parameter in
% ARGS that does not match CONF back to ARGS instead of producing an
% error.
%
% Example::
% The function can be used to parse a list of arguments
% passed to a MATLAB functions:
%
% function myFunction(x,y,z,varargin)
% conf.parameterName = defaultValue ;
% conf = vl_argparse(conf, varargin)
%
% If only a subset of the options should be parsed, for example
% because the other options are interpreted by a subroutine, then
% use the form
%
% [conf, varargin] = vl_argparse(conf, varargin)
%
% that copies back to VARARGIN any unknown parameter.
%
% See also: VL_OVERRIDE(), VL_HELP().
% Authors: Andrea Vedaldi
% Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
% All rights reserved.
%
% This file is part of the VLFeat library and is made available under
% the terms of the BSD license (see the COPYING file).
%if ~isstruct(conf), error('CONF must be a structure') ; end
if nargin == 1
args = { };
return;
end
if length(varargin) > 0, args = {args, varargin{:}} ; end
if length(args) == 0
return;
end
remainingArgs = {} ;
names = fieldnames(conf) ;
if mod(length(args),2) == 1
error('Parameter-value pair expected (missing value?).') ;
end
for ai = 1:2:length(args)
paramName = args{ai} ;
if ~ischar(paramName)
error('The name of the parameter number %d is not a string.', (ai-1)/2+1) ;
end
value = args{ai+1} ;
if isfield(conf,paramName)
conf.(paramName) = value ;
else
% try case-insensitive
i = find(strcmpi(paramName, names)) ;
if isempty(i)
if nargout < 2
error('Unknown parameter ''%s''.', paramName) ;
else
remainingArgs(end+1:end+2) = args(ai:ai+1) ;
end
else
conf.(names{i}) = value ;
end
end
end
args = remainingArgs ;
if isempty(args)
args = { };
end