1 ## Copyright (C) 2008-2012 Bill Denney
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs})
21 ## @deftypefnx {Function File} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "string")
22 ## @deftypefnx {Function File} {@var{msgstruct} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "struct")
23 ## Return an appropriate error message string (or structure) if the
24 ## number of inputs requested is invalid.
26 ## This is useful for checking to see that the number of input arguments
27 ## supplied to a function is within an acceptable range.
28 ## @seealso{nargoutchk, narginchk, error, nargin, nargout}
31 ## Author: Bill Denney <bill@denney.ws>
33 function msg = nargchk (minargs, maxargs, nargs, outtype = "string")
35 if (nargin < 3 || nargin > 4)
37 elseif (minargs > maxargs)
38 error ("nargchk: MINARGS must be <= MAXARGS");
39 elseif (! any (strcmpi (outtype, {"string", "struct"})))
40 error ('nargchk: output type must be either "string" or "struct"');
41 elseif (! (isscalar (minargs) && isscalar (maxargs) && isscalar (nargs)))
42 error ("nargchk: MINARGS, MAXARGS, and NARGS must be scalars");
45 msg = struct ("message", "", "identifier", "");
47 msg.message = "not enough input arguments";
48 msg.identifier = "Octave:nargchk:not-enough-inputs";
49 elseif (nargs > maxargs)
50 msg.message = "too many input arguments";
51 msg.identifier = "Octave:nargchk:too-many-inputs";
54 if (strcmpi (outtype, "string"))
56 elseif (isempty (msg.message))
57 ## Compatability: Matlab returns a 0x1 empty struct when nargchk passes
58 msg = resize (msg, 0, 1);
65 %!shared stnul, stmin, stmax
66 %! stnul = resize (struct ("message", "", "identifier", ""), 0, 1);
67 %! stmin = struct ("message", "not enough input arguments",
68 %! "identifier", "Octave:nargchk:not-enough-inputs");
69 %! stmax = struct ("message", "too many input arguments",
70 %! "identifier", "Octave:nargchk:too-many-inputs");
71 %!assert (nargchk (0, 1, 0), "")
72 %!assert (nargchk (0, 1, 1), "")
73 %!assert (nargchk (1, 1, 0), "not enough input arguments")
74 %!assert (nargchk (0, 1, 2), "too many input arguments")
75 %!assert (nargchk (0, 1, 2, "string"), "too many input arguments")
77 %!assert (isequal (nargchk (0, 1, 0, "struct"), stnul))
78 %!assert (isequal (nargchk (0, 1, 1, "struct"), stnul))
79 %!assert (nargchk (1, 1, 0, "struct"), stmin)
80 %!assert (nargchk (0, 1, 2, "struct"), stmax)