# HG changeset patch # User Peter L. Sondergaard # Date 1231999566 18000 # Node ID d65b33e55d4060c955c8186bc0872ff671dfc218 # Parent 93cf10950334908b990b504e0d14d14430303176 nargchk.m: improve compatibility; new tests diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2009-01-15 Peter L. Søndergaard + + * general/nargoutchk.m: Doc fix. + * general/nargchk.m: Improve compatibility. New tests. + 2008-01-15 Rafael Laboissiere * gethelp.cc: Include . diff --git a/scripts/general/nargchk.m b/scripts/general/nargchk.m --- a/scripts/general/nargchk.m +++ b/scripts/general/nargchk.m @@ -1,5 +1,4 @@ -## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2005, 2006, 2007 -## John W. Eaton +## Copyright (C) 2008 Bill Denney ## ## This file is part of Octave. ## @@ -18,34 +17,68 @@ ## . ## -*- texinfo -*- -## @deftypefn {Function File} {} nargchk (@var{nargin_min}, @var{nargin_max}, @var{n}) -## If @var{n} is in the range @var{nargin_min} through @var{nargin_max} -## inclusive, return the empty matrix. Otherwise, return a message -## indicating whether @var{n} is too large or too small. +## @deftypefn {Function File} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}) +## @deftypefnx {Function File} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "string") +## @deftypefnx {Function File} {@var{msgstruct} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "struct") +## Return an appropriate error message string (or structure) if the +## number of inputs requested is invalid. ## -## This is useful for checking to see that the number of arguments supplied -## to a function is within an acceptable range. +## This is useful for checking to see that the number of input arguments +## supplied to a function is within an acceptable range. ## @seealso{nargoutchk, error, nargin, nargout} ## @end deftypefn -## Author: jwe +## Author: Bill Denney + +function msg = nargchk (mina, maxa, narg, outtype) -function retval = nargchk (nargin_min, nargin_max, n) - - if (nargin != 3) + if (nargin < 3 || nargin > 4) print_usage (); + elseif (mina > maxa) + error ("nargchk: minargs must be <= maxargs"); + elseif (nargin == 3) + outtype = "string"; + elseif (! any (strcmpi (outtype, {"string" "struct"}))) + error ("nargchk: output type must be either string or struct"); + elseif (! (isscalar (mina) && isscalar (maxa) && isscalar (narg))) + error ("nargchk: mina, maxa, and narg must be scalars"); endif - if (nargin_min > nargin_max) - error ("nargchk: nargin_min > nargin_max"); + msg = struct ("message", "", "identifier", ""); + if (narg < mina) + msg.message = "not enough input arguments"; + msg.identifier = "Octave:nargchk:not-enough-inputs"; + elseif (narg > maxa) + msg.message = "too many input arguments"; + msg.identifier = "Octave:nargchk:too-many-inputs"; endif - if (n < nargin_min) - retval = "nargchk: N is less than nargin_min"; - elseif (n > nargin_max) - retval = "nargchk: N is greater than nargin_max"; + if (strcmpi (outtype, "string")) + msg = msg.message; else - retval = []; + if (isempty (msg.message)) + msg = struct ([]); + endif + ## FIXME: remove the error below if error is modified to accept + ## struct inputs + error ("nargchk: error does not yet support struct inputs") endif endfunction + +## Tests +%!shared stmin, stmax +%! stmin = struct ("message", "not enough input arguments", +%! "identifier", "Octave:nargchk:not-enough-inputs"); +%! stmax = struct ("message", "too many input arguments", +%! "identifier", "Octave:nargchk:too-many-inputs"); +%!assert (nargchk (0, 1, 0), "") +%!assert (nargchk (0, 1, 1), "") +%!assert (nargchk (1, 1, 0), "not enough input arguments") +%!assert (nargchk (0, 1, 2), "too many input arguments") +%!assert (nargchk (0, 1, 2, "string"), "too many input arguments") +## Struct outputs +#%!assert (nargchk (0, 1, 0, "struct"), struct([])) +#%!assert (nargchk (0, 1, 1, "struct"), struct([])) +#%!assert (nargchk (1, 1, 0, "struct"), stmin) +#%!assert (nargchk (0, 1, 2, "struct"), stmax) diff --git a/scripts/general/nargoutchk.m b/scripts/general/nargoutchk.m --- a/scripts/general/nargoutchk.m +++ b/scripts/general/nargoutchk.m @@ -23,8 +23,8 @@ ## Return an appropriate error message string (or structure) if the ## number of outputs requested is invalid. ## -## This is useful for checking to see that the number of arguments supplied -## to a function is within an acceptable range. +## This is useful for checking to see that the number of output +## arguments supplied to a function is within an acceptable range. ## @seealso{nargchk, error, nargout, nargin} ## @end deftypefn