changeset 13852:e36817e2ee60

New function narginchk
author Carnë Draug <carandraug+dev@gmail.com>
date Fri, 28 Oct 2011 17:31:43 +0100
parents 5f96b91b4e0c
children ef7bf5af9faf
files doc/interpreter/func.txi scripts/general/module.mk scripts/general/nargchk.m scripts/general/narginchk.m scripts/general/nargoutchk.m
diffstat 5 files changed, 70 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/doc/interpreter/func.txi	Thu Oct 27 23:00:58 2011 -0700
+++ b/doc/interpreter/func.txi	Fri Oct 28 17:31:43 2011 +0100
@@ -359,11 +359,13 @@
 which stops the function execution and prints a message about the correct
 way to call the function whenever the number of inputs is wrong.
 
-For compatibility with @sc{matlab}, @code{nargchk} and @code{nargoutchk} are
-available which provide similar error checking.
+For compatibility with @sc{matlab}, @code{nargchk}, @code{narginchk} and
+@code{nargoutchk} are available which provide similar error checking.
 
 @DOCSTRING(nargchk)
 
+@DOCSTRING(narginchk)
+
 @DOCSTRING(nargoutchk)
 
 @anchor{doc-varargin} @anchor{doc-varargout}
--- a/scripts/general/module.mk	Thu Oct 27 23:00:58 2011 -0700
+++ b/scripts/general/module.mk	Fri Oct 28 17:31:43 2011 +0100
@@ -53,6 +53,7 @@
   general/loadobj.m \
   general/logspace.m \
   general/nargchk.m \
+  general/narginchk.m \
   general/nargoutchk.m \
   general/nthargout.m \
   general/nextpow2.m \
--- a/scripts/general/nargchk.m	Thu Oct 27 23:00:58 2011 -0700
+++ b/scripts/general/nargchk.m	Fri Oct 28 17:31:43 2011 +0100
@@ -25,7 +25,7 @@
 ##
 ## 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}
+## @seealso{nargoutchk, narginchk, error, nargin, nargout}
 ## @end deftypefn
 
 ## Author: Bill Denney <bill@denney.ws>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/general/narginchk.m	Fri Oct 28 17:31:43 2011 +0100
@@ -0,0 +1,63 @@
+## Copyright (C) 2011 Carnë Draug
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} narginchk (@var{minargs}, @var{maxargs})
+## Check for correct number of arguments.
+##
+## This function returns an error unless the number of arguments in its caller
+## is between the values of @var{minargs} and @var{maxargs}. It does nothing
+## otherwise.
+##
+## Both @var{minargs} and @var{maxargs} need to be a numeric scalar. Zero, Inf
+## and negative are all valid, and they can have the same value.
+##
+## Note that this function evaluates the value of @code{nargin} on the caller so
+## its value must have not been tampered with.
+##
+## @seealso{nargchk, nargoutchk, error, nargout, nargin}
+## @end deftypefn
+
+## Author: Carnë Draug <carandraug+dev@gmail.com>
+
+function narginchk (minargs, maxargs)
+
+  ## it requires always two arguments (can't specify only min)
+  ## zero, negative and inf are all valid arguments and they can be equal
+  ## thanks to Oldak in ##matlab for the help in checking these corner cases
+  ## tested compatibility in version 2011b
+
+  if (nargin != 2)
+    print_usage;
+  elseif (!isnumeric (minargs) || !isscalar (minargs))
+    error ("minargs must be a numeric scalar");
+  elseif (!isnumeric (maxargs) || !isscalar (maxargs))
+    error ("maxargs must be a numeric scalar");
+  elseif (minargs > maxargs)
+    error ("minargs cannot be larger than maxargs")
+  endif
+
+  args = evalin ("caller", "nargin;");
+
+  if (args < minargs)
+    error ("Not enough input arguments.");
+  elseif (args > maxargs)
+    error ("Too many input arguments.");
+  endif
+
+endfunction
--- a/scripts/general/nargoutchk.m	Thu Oct 27 23:00:58 2011 -0700
+++ b/scripts/general/nargoutchk.m	Fri Oct 28 17:31:43 2011 +0100
@@ -25,7 +25,7 @@
 ##
 ## 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}
+## @seealso{nargchk, narginchk, error, nargout, nargin}
 ## @end deftypefn
 
 ## Author: Bill Denney <bill@denney.ws>