annotate scripts/miscellaneous/narginchk.m @ 31103:7797481038fc stable

inputParser: Add some tests for bug #62639.
author Markus Mützel <markus.muetzel@gmx.de>
date Tue, 21 Jun 2022 15:43:27 +0200
parents 796f54d4ddbf
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 2012-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
7 ##
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
8 ## This file is part of Octave.
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
14 ##
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
19 ##
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
25
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20726
diff changeset
27 ## @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
28 ## Check for correct number of input arguments.
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
29 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ## 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
31 ## 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
32 ## nothing.
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ## 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
35 ## 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
36 ## @var{maxargs} may be equal.
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
37 ##
13854
a09958da79b8 * narginchk.m: Style and doc fixes.
John W. Eaton <jwe@octave.org>
parents: 13852
diff changeset
38 ## Note that this function evaluates @code{nargin} on the caller.
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
39 ##
19068
be8a12acb20a Deprecate nargchk in favor of narginchk.
Rik <rik@octave.org>
parents: 17744
diff changeset
40 ## @seealso{nargoutchk, error, nargout, nargin}
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
41 ## @end deftypefn
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
42
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
43 function narginchk (minargs, maxargs)
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
44
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
45 if (nargin != 2)
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 27978
diff changeset
46 print_usage ();
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
47 elseif (! isnumeric (minargs) || ! isscalar (minargs))
22760
c4d80b9d2898 maint: Capitalize variable names appearing in error() messages of m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
48 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
49 elseif (! isnumeric (maxargs) || ! isscalar (maxargs))
22760
c4d80b9d2898 maint: Capitalize variable names appearing in error() messages of m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
50 error ("narginchk: MAXARGS must be a numeric scalar");
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
51 elseif (minargs > maxargs)
22760
c4d80b9d2898 maint: Capitalize variable names appearing in error() messages of m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
52 error ("narginchk: MINARGS cannot be larger than MAXARGS");
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
53 endif
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
54
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
55 args = evalin ("caller", "nargin;");
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
56
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
57 if (args < minargs)
20726
25d676f9619c Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents: 20158
diff changeset
58 error ("narginchk: not enough input arguments");
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
59 elseif (args > maxargs)
20726
25d676f9619c Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents: 20158
diff changeset
60 error ("narginchk: too many input arguments");
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
61 endif
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
62
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff changeset
63 endfunction
13855
5050e92dc25a * narginchk.m: New tests.
John W. Eaton <jwe@octave.org>
parents: 13854
diff changeset
64
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
65
13855
5050e92dc25a * narginchk.m: New tests.
John W. Eaton <jwe@octave.org>
parents: 13854
diff changeset
66 %!function f (nargs, varargin)
5050e92dc25a * narginchk.m: New tests.
John W. Eaton <jwe@octave.org>
parents: 13854
diff changeset
67 %! narginchk (nargs(1), nargs(2));
5050e92dc25a * narginchk.m: New tests.
John W. Eaton <jwe@octave.org>
parents: 13854
diff changeset
68 %!endfunction
5050e92dc25a * narginchk.m: New tests.
John W. Eaton <jwe@octave.org>
parents: 13854
diff changeset
69
5050e92dc25a * narginchk.m: New tests.
John W. Eaton <jwe@octave.org>
parents: 13854
diff changeset
70 %!error <too many input arguments> f([0,0])
5050e92dc25a * narginchk.m: New tests.
John W. Eaton <jwe@octave.org>
parents: 13854
diff changeset
71 %!error <not enough input arguments> f([3, 3], 1)
5050e92dc25a * narginchk.m: New tests.
John W. Eaton <jwe@octave.org>
parents: 13854
diff changeset
72
5050e92dc25a * narginchk.m: New tests.
John W. Eaton <jwe@octave.org>
parents: 13854
diff changeset
73 %!test
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21546
diff changeset
74 %! f([1,1]);
13855
5050e92dc25a * narginchk.m: New tests.
John W. Eaton <jwe@octave.org>
parents: 13854
diff changeset
75 %!test
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21546
diff changeset
76 %! f([1,5], 2, 3, 4, 5);