]> Creatis software - CreaPhase.git/blob - octave_packages/general-1.3.1/@inputParser/addOptional.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / general-1.3.1 / @inputParser / addOptional.m
1 ## Copyright (C) 2011 CarnĂ« Draug <carandraug+dev@gmail.com>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{parser} =} addOptional (@var{parser}, @var{argname}, @var{default})
18 ## @deftypefnx {Function File} {@var{parser} =} @var{parser}.addOptional (@var{argname}, @var{default})
19 ## @deftypefnx {Function File} {@var{parser} =} addOptional (@var{parser}, @var{argname}, @var{default}, @var{validator})
20 ## @deftypefnx {Function File} {@var{parser} =} @var{parser}.addOptional (@var{argname}, @var{default}, @var{validator})
21 ## Add new optional argument to the object @var{parser} of the class inputParser
22 ## to implement an ordered arguments type of API 
23 ##
24 ## @var{argname} must be a string with the name of the new argument. The order
25 ## in which new arguments are added with @command{addOptional}, represents the
26 ## expected order of arguments.
27 ##
28 ## @var{default} will be the value used when the argument is not specified.
29 ##
30 ## @var{validator} is an optional anonymous function to validate the given values
31 ## for the argument with name @var{argname}. Alternatively, a function name
32 ## can be used.
33 ##
34 ## See @command{help inputParser} for examples.
35 ##
36 ## @emph{Note}: if a string argument does not validate, it will be considered a
37 ## ParamValue key. If an optional argument is not given a validator, anything
38 ## will be valid, and so any string will be considered will be the value of the
39 ## optional argument (in @sc{matlab}, if no validator is given and argument is
40 ## a string it will also be considered a ParamValue key).
41 ##
42 ## @seealso{inputParser, @@inputParser/addParamValue, @@inputParser/addSwitch,
43 ## @@inputParser/addParamValue, @@inputParser/addRequired, @@inputParser/parse}
44 ## @end deftypefn
45
46 function inPar = addOptional (inPar, name, def, val)
47
48   ## check @inputParser/subsref for the actual code
49   if (nargin == 3)
50     inPar = subsref (inPar, substruct(
51                                       '.' , 'addOptional',
52                                       '()', {name, def}
53                                       ));
54   elseif (nargin == 4)
55     inPar = subsref (inPar, substruct(
56                                       '.' , 'addOptional',
57                                       '()', {name, def, val}
58                                       ));
59   else
60     print_usage;
61   endif
62
63 endfunction