Mercurial > octave
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 |
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 | 7 ## |
8 ## This file is part of Octave. | |
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 | 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 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
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 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
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 | 25 |
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 | 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 | 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 | 39 ## |
19068
be8a12acb20a
Deprecate nargchk in favor of narginchk.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
40 ## @seealso{nargoutchk, error, nargout, nargin} |
13852 | 41 ## @end deftypefn |
42 | |
43 function narginchk (minargs, maxargs) | |
44 | |
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 | 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 | 53 endif |
54 | |
55 args = evalin ("caller", "nargin;"); | |
56 | |
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 | 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 | 61 endif |
62 | |
63 endfunction | |
13855 | 64 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
65 |
13855 | 66 %!function f (nargs, varargin) |
67 %! narginchk (nargs(1), nargs(2)); | |
68 %!endfunction | |
69 | |
70 %!error <too many input arguments> f([0,0]) | |
71 %!error <not enough input arguments> f([3, 3], 1) | |
72 | |
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 | 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); |