Mercurial > hg > octave-lyh
annotate scripts/general/nargchk.m @ 12705:4972eb61c6d6
Fix bug with error() not accepting an empty struct input (Bug #33428).
* nargchk.m, nargoutchk.m: Return scalar empty struct if there is no error.
* error.cc: Accept empty struct as input with no error, per Matlab.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 04 Jun 2011 16:10:38 -0700 |
parents | fd0a3ac60b0e |
children | a4d1581f9e72 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2008-2011 Bill Denney |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
710 | 18 |
3371 | 19 ## -*- texinfo -*- |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9204
diff
changeset
|
20 ## @deftypefn {Function File} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}) |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "string") |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{msgstruct} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "struct") |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
23 ## Return an appropriate error message string (or structure) if the |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
24 ## number of inputs requested is invalid. |
3426 | 25 ## |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
26 ## 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
|
27 ## supplied to a function is within an acceptable range. |
7658
1ce6460aebdf
nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
7017
diff
changeset
|
28 ## @seealso{nargoutchk, error, nargin, nargout} |
3371 | 29 ## @end deftypefn |
710 | 30 |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
31 ## Author: Bill Denney <bill@denney.ws> |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
32 |
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
|
33 function msg = nargchk (minargs, maxargs, nargs, outtype) |
2314 | 34 |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
35 if (nargin < 3 || nargin > 4) |
6046 | 36 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
|
37 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
|
38 error ("nargchk: MINARGS must be <= MAXARGS"); |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
39 elseif (nargin == 3) |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
40 outtype = "string"; |
9204
1f47a9404d93
nargchk.m: don't generate error if output is struct
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
41 elseif (! any (strcmpi (outtype, {"string", "struct"}))) |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
42 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
|
43 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
|
44 error ("nargchk: MINARGS, MAXARGS, and NARGS must be scalars"); |
710 | 45 endif |
46 | |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
47 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
|
48 if (nargs < minargs) |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
49 msg.message = "not enough input arguments"; |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
50 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
|
51 elseif (nargs > maxargs) |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
52 msg.message = "too many input arguments"; |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
53 msg.identifier = "Octave:nargchk:too-many-inputs"; |
710 | 54 endif |
55 | |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
56 if (strcmpi (outtype, "string")) |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
57 msg = msg.message; |
9204
1f47a9404d93
nargchk.m: don't generate error if output is struct
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
58 elseif (isempty (msg.message)) |
12705
4972eb61c6d6
Fix bug with error() not accepting an empty struct input (Bug #33428).
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
59 msg = struct (); |
710 | 60 endif |
61 | |
62 endfunction | |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
63 |
12705
4972eb61c6d6
Fix bug with error() not accepting an empty struct input (Bug #33428).
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
64 |
8522
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
65 ## Tests |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
66 %!shared stmin, stmax |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
67 %! stmin = struct ("message", "not enough input arguments", |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
68 %! "identifier", "Octave:nargchk:not-enough-inputs"); |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
69 %! stmax = struct ("message", "too many input arguments", |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
70 %! "identifier", "Octave:nargchk:too-many-inputs"); |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
71 %!assert (nargchk (0, 1, 0), "") |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
72 %!assert (nargchk (0, 1, 1), "") |
d65b33e55d40
nargchk.m: improve compatibility; new tests
Peter L. Sondergaard <peter@sonderport.dk>
parents:
7658
diff
changeset
|
73 %!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
|
74 %!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
|
75 %!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
|
76 ## Struct outputs |
12705
4972eb61c6d6
Fix bug with error() not accepting an empty struct input (Bug #33428).
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
77 %!assert (nargchk (0, 1, 0, "struct"), struct()) |
4972eb61c6d6
Fix bug with error() not accepting an empty struct input (Bug #33428).
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
78 %!assert (nargchk (0, 1, 1, "struct"), struct()) |
9204
1f47a9404d93
nargchk.m: don't generate error if output is struct
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
79 %!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
|
80 %!assert (nargchk (0, 1, 2, "struct"), stmax) |