Mercurial > octave-nkf
annotate scripts/miscellaneous/inputname.m @ 20610:a61f0d6beb71
inputParser: do not perform validation of default values (bug #45837)
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Thu, 08 Oct 2015 20:15:19 +0100 |
parents | d5b877b86ed4 |
children |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19630
diff
changeset
|
1 ## Copyright (C) 2004-2015 Paul Kienzle |
5837 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
7016 | 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 ## Original version by Paul Kienzle distributed as free software in the | |
20 ## public domain. | |
5837 | 21 |
22 ## -*- texinfo -*- | |
23 ## @deftypefn {Function File} {} inputname (@var{n}) | |
10091
a115046d462d
inputname: compatibility fix
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
24 ## Return the name of the @var{n}-th argument to the calling function. |
19229 | 25 ## |
10091
a115046d462d
inputname: compatibility fix
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
26 ## If the argument is not a simple variable name, return an empty string. |
19229 | 27 ## @code{inputname} may only be used within a function body, not at the |
28 ## command line. | |
29 ## @seealso{nargin, nthargout} | |
5837 | 30 ## @end deftypefn |
31 | |
32 function s = inputname (n) | |
7125 | 33 |
19229 | 34 if (nargin != 1) |
7125 | 35 print_usage (); |
36 endif | |
37 | |
19759
5f2c0ca0ef51
Ensure that numbers passed to integer *printf format codes are integers (bug #44245).
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
38 s = evalin ("caller", sprintf ("__varval__ (\".argn.\"){%d};", fix (n))); |
19229 | 39 ## For compatibility with Matlab, |
40 ## return empty string if argument name is not a valid identifier. | |
41 if (! isvarname (s)) | |
42 s = ""; | |
43 endif | |
44 | |
5837 | 45 endfunction |
46 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
47 |
5837 | 48 ## Warning: heap big magic in the following tests!!! |
49 ## The test function builds a private context for each | |
50 ## test, with only the specified values shared between | |
51 ## them. It does this using the following template: | |
52 ## | |
53 ## function [<shared>] = testfn(<shared>) | |
54 ## <test> | |
55 ## | |
56 ## To test inputname, I need a function context invoked | |
57 ## with known parameter names. So define a couple of | |
58 ## shared parameters, et voila!, the test is trivial. | |
19627
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
59 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
60 %!shared hello, worldly |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
61 %!assert (inputname (1), "hello") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
62 %!assert (inputname (2), "worldly") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
63 |
20462
d5b877b86ed4
inputname.m: Rename BIST function foo to __foo__ to avoid name collisions.
Rik <rik@octave.org>
parents:
19759
diff
changeset
|
64 %!function r = __foo__ (x, y) |
18201
ec87e965c246
allow inputname to work correctly with feval (bug #35497)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
65 %! r = inputname (2); |
ec87e965c246
allow inputname to work correctly with feval (bug #35497)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
66 %!endfunction |
20462
d5b877b86ed4
inputname.m: Rename BIST function foo to __foo__ to avoid name collisions.
Rik <rik@octave.org>
parents:
19759
diff
changeset
|
67 %!assert (__foo__ (pi, e), "e"); |
d5b877b86ed4
inputname.m: Rename BIST function foo to __foo__ to avoid name collisions.
Rik <rik@octave.org>
parents:
19759
diff
changeset
|
68 %!assert (feval (@__foo__, pi, e), "e"); |
19229 | 69 |
70 %!error inputname () | |
71 %!error inputname (1,2) | |
72 |