]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/narginchk.m
update packages
[CreaPhase.git] / octave_packages / m / general / narginchk.m
1 ## Copyright (C) 2012 CarnĂ« Draug
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} narginchk (@var{minargs}, @var{maxargs})
21 ## Check for correct number of arguments or generate an error message if
22 ## the number of arguments in the calling function is outside the range
23 ## @var{minargs} and @var{maxargs}.  Otherwise, do nothing.
24 ##
25 ## Both @var{minargs} and @var{maxargs} need to be scalar numeric
26 ## values.  Zero, Inf and negative values are all allowed, and
27 ## @var{minargs} and @var{maxargs} may be equal.
28 ##
29 ## Note that this function evaluates @code{nargin} on the caller.
30 ##
31 ## @seealso{nargchk, nargoutchk, error, nargout, nargin}
32 ## @end deftypefn
33
34 ## Author: CarnĂ« Draug <carandraug+dev@gmail.com>
35
36 function narginchk (minargs, maxargs)
37
38   if (nargin != 2)
39     print_usage;
40   elseif (!isnumeric (minargs) || !isscalar (minargs))
41     error ("minargs must be a numeric scalar");
42   elseif (!isnumeric (maxargs) || !isscalar (maxargs))
43     error ("maxargs must be a numeric scalar");
44   elseif (minargs > maxargs)
45     error ("minargs cannot be larger than maxargs")
46   endif
47
48   args = evalin ("caller", "nargin;");
49
50   if (args < minargs)
51     error ("not enough input arguments");
52   elseif (args > maxargs)
53     error ("too many input arguments");
54   endif
55
56 endfunction
57
58 %!function f (nargs, varargin)
59 %! narginchk (nargs(1), nargs(2));
60 %!endfunction
61
62 %!error <too many input arguments> f([0,0])
63 %!error <not enough input arguments> f([3, 3], 1)
64
65 %!test
66 %! f([1,1])
67
68 %!test
69 %! f([1,5], 2, 3, 4, 5)