Mercurial > octave
annotate scripts/general/narginchk.m @ 23219:3ac9f9ecfae5 stable
maint: Update copyright dates.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 22 Feb 2017 12:39:29 -0500 |
parents | e9a0469dedd9 |
children | 092078913d54 |
rev | line source |
---|---|
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
1 ## Copyright (C) 2012-2017 Carnë Draug |
13852 | 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 -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20726
diff
changeset
|
20 ## @deftypefn {} {} narginchk (@var{minargs}, @var{maxargs}) |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
21 ## Check for correct number of input arguments. |
13852 | 22 ## |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
23 ## Generate an error message if the number of arguments in the calling function |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
24 ## is outside the range @var{minargs} and @var{maxargs}. Otherwise, do |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
25 ## nothing. |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
26 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
27 ## Both @var{minargs} and @var{maxargs} must be scalar numeric values. Zero, |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
28 ## Inf, and negative values are all allowed, and @var{minargs} and |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
29 ## @var{maxargs} may be equal. |
13852 | 30 ## |
13854
a09958da79b8
* narginchk.m: Style and doc fixes.
John W. Eaton <jwe@octave.org>
parents:
13852
diff
changeset
|
31 ## Note that this function evaluates @code{nargin} on the caller. |
13852 | 32 ## |
19068
be8a12acb20a
Deprecate nargchk in favor of narginchk.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
33 ## @seealso{nargoutchk, error, nargout, nargin} |
13852 | 34 ## @end deftypefn |
35 | |
36 ## Author: Carnë Draug <carandraug+dev@gmail.com> | |
37 | |
38 function narginchk (minargs, maxargs) | |
39 | |
40 if (nargin != 2) | |
41 print_usage; | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
42 elseif (! isnumeric (minargs) || ! isscalar (minargs)) |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
43 error ("narginchk: minargs must be a numeric scalar"); |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
44 elseif (! isnumeric (maxargs) || ! isscalar (maxargs)) |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
45 error ("narginchk: maxargs must be a numeric scalar"); |
13852 | 46 elseif (minargs > maxargs) |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
47 error ("narginchk: minargs cannot be larger than maxargs"); |
13852 | 48 endif |
49 | |
50 args = evalin ("caller", "nargin;"); | |
51 | |
52 if (args < minargs) | |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
53 error ("narginchk: not enough input arguments"); |
13852 | 54 elseif (args > maxargs) |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
55 error ("narginchk: too many input arguments"); |
13852 | 56 endif |
57 | |
58 endfunction | |
13855 | 59 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
60 |
13855 | 61 %!function f (nargs, varargin) |
62 %! narginchk (nargs(1), nargs(2)); | |
63 %!endfunction | |
64 | |
65 %!error <too many input arguments> f([0,0]) | |
66 %!error <not enough input arguments> f([3, 3], 1) | |
67 | |
68 %!test | |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
69 %! f([1,1]); |
13855 | 70 %!test |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
71 %! f([1,5], 2, 3, 4, 5); |