Mercurial > octave
annotate scripts/miscellaneous/nargchk.m @ 31247:3dae836c598c
doc: Expand and edit documentation for memoization (bug #60860)
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Fri, 30 Sep 2022 06:38:59 -0400 |
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) 2008-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/>. |
2313 | 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 |
2313 | 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. |
2313 | 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. |
2313 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 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 ######################################################################## |
710 | 25 |
3371 | 26 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
27 ## @deftypefn {} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
28 ## @deftypefnx {} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "string") |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
29 ## @deftypefnx {} {@var{msgstruct} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "struct") |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
30 ## Return an appropriate error message string (or structure) if the number of |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
31 ## inputs requested is invalid. |
3426 | 32 ## |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
33 ## This is useful for checking to see that the number of input arguments |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
34 ## supplied to a function is within an acceptable range. |
19112
bc640d4678fb
Undeprecate nargchk, but add note that narginchk is preferred.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
35 ## |
bc640d4678fb
Undeprecate nargchk, but add note that narginchk is preferred.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
36 ## @strong{Caution}: @code{nargchk} is scheduled for deprecation. Use |
bc640d4678fb
Undeprecate nargchk, but add note that narginchk is preferred.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
37 ## @code{narginchk} in all new code. |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
38 ## @seealso{narginchk, nargoutchk, error, nargin, nargout} |
3371 | 39 ## @end deftypefn |
710 | 40 |
12881
49553ea11764
Return 0x1 empty struct when nargchk, nargoutcheck passes for ML compatability (bug #33808)
Rik <octave@nomad.inbox5.com>
parents:
12865
diff
changeset
|
41 function msg = nargchk (minargs, maxargs, nargs, outtype = "string") |
2314 | 42 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
43 if (nargin < 3) |
6046 | 44 print_usage (); |
11470
eb9e0b597d61
Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
45 elseif (minargs > maxargs) |
eb9e0b597d61
Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
46 error ("nargchk: MINARGS must be <= MAXARGS"); |
9204
1f47a9404d93
nargchk.m: don't generate error if output is struct
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
47 elseif (! any (strcmpi (outtype, {"string", "struct"}))) |
12881
49553ea11764
Return 0x1 empty struct when nargchk, nargoutcheck passes for ML compatability (bug #33808)
Rik <octave@nomad.inbox5.com>
parents:
12865
diff
changeset
|
48 error ('nargchk: output type must be either "string" or "struct"'); |
11470
eb9e0b597d61
Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
49 elseif (! (isscalar (minargs) && isscalar (maxargs) && isscalar (nargs))) |
eb9e0b597d61
Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
50 error ("nargchk: MINARGS, MAXARGS, and NARGS must be scalars"); |
710 | 51 endif |
52 | |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
53 msg = struct ("message", "", "identifier", ""); |
11470
eb9e0b597d61
Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
54 if (nargs < minargs) |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
55 msg.message = "not enough input arguments"; |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
56 msg.identifier = "Octave:nargchk:not-enough-inputs"; |
11470
eb9e0b597d61
Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
57 elseif (nargs > maxargs) |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
58 msg.message = "too many input arguments"; |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
59 msg.identifier = "Octave:nargchk:too-many-inputs"; |
710 | 60 endif |
61 | |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
62 if (strcmpi (outtype, "string")) |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
63 msg = msg.message; |
12881
49553ea11764
Return 0x1 empty struct when nargchk, nargoutcheck passes for ML compatability (bug #33808)
Rik <octave@nomad.inbox5.com>
parents:
12865
diff
changeset
|
64 elseif (isempty (msg.message)) |
27956
2310164737b3
fix many spelling errors (bug #57613)
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
65 ## Compatibility: Matlab returns a 0x1 empty struct when nargchk passes |
12881
49553ea11764
Return 0x1 empty struct when nargchk, nargoutcheck passes for ML compatability (bug #33808)
Rik <octave@nomad.inbox5.com>
parents:
12865
diff
changeset
|
66 msg = resize (msg, 0, 1); |
710 | 67 endif |
68 | |
69 endfunction | |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
70 |
12705
4972eb61c6d6
Fix bug with error() not accepting an empty struct input (Bug #33428).
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
71 |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
72 ## Tests |
12865
a4d1581f9e72
Return correct struct for nargchk, nargoutchk (Bug #33808).
Rik <octave@nomad.inbox5.com>
parents:
12705
diff
changeset
|
73 %!shared stnul, stmin, stmax |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 %! stnul = resize (struct ("message", "", "identifier", ""), 0, 1); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
75 %! stmin = struct ("message", "not enough input arguments", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
76 %! "identifier", "Octave:nargchk:not-enough-inputs"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
77 %! stmax = struct ("message", "too many input arguments", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
78 %! "identifier", "Octave:nargchk:too-many-inputs"); |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
79 %!assert (nargchk (0, 1, 0), "") |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
80 %!assert (nargchk (0, 1, 1), "") |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
81 %!assert (nargchk (1, 1, 0), "not enough input arguments") |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
82 %!assert (nargchk (0, 1, 2), "too many input arguments") |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
83 %!assert (nargchk (0, 1, 2, "string"), "too many input arguments") |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
84 ## Struct outputs |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
85 %!assert (nargchk (0, 1, 0, "struct"), stnul) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
86 %!assert (nargchk (0, 1, 1, "struct"), stnul) |
9204
1f47a9404d93
nargchk.m: don't generate error if output is struct
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
87 %!assert (nargchk (1, 1, 0, "struct"), stmin) |
1f47a9404d93
nargchk.m: don't generate error if output is struct
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
88 %!assert (nargchk (0, 1, 2, "struct"), stmax) |