changeset 26214:c0ac6fc191d7

allow inputname to return non-ID arguments (bug #55213) * inputname.m: New optional argument, IDS_ONLY. Default value is true. If false, return text of argument, even if it is not a valid variable name. * assert.m, test.m: Call inputname with new IDS_ONLY argument = false.
author John W. Eaton <jwe@octave.org>
date Wed, 12 Dec 2018 21:44:36 -0500
parents ff0eadb417ec
children 39e84bf92d18
files scripts/miscellaneous/inputname.m scripts/testfun/assert.m scripts/testfun/test.m
diffstat 3 files changed, 12 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/inputname.m	Wed Dec 12 17:22:08 2018 -0800
+++ b/scripts/miscellaneous/inputname.m	Wed Dec 12 21:44:36 2018 -0500
@@ -21,6 +21,7 @@
 
 ## -*- texinfo -*-
 ## @deftypefn {} {} inputname (@var{n})
+## @deftypefnx {} {} inputname (@var{n}, @var{ids_only})
 ## Return the name of the @var{n}-th argument to the calling function.
 ##
 ## If the argument is not a simple variable name, return an empty string.  As
@@ -29,12 +30,17 @@
 ##
 ## @code{inputname} is only useful within a function.  When used at the command
 ## line it always returns an empty string.
+##
+## By default, return an empty string if the @var{n}-th argument is not
+## a valid variable name.  If the optional argument @var{ids_only} is
+## false, return the text of the argument even if it is not a valid
+## variable name.
 ## @seealso{nargin, nthargout}
 ## @end deftypefn
 
-function name = inputname (n)
+function name = inputname (n, ids_only = true)
 
-  if (nargin != 1)
+  if (nargin > 2)
     print_usage ();
   endif
 
@@ -47,7 +53,7 @@
 
   ## For compatibility with Matlab,
   ## return empty string if argument name is not a valid identifier.
-  if (! isvarname (name))
+  if (ids_only && ! isvarname (name))
     name = "";
   endif
 
--- a/scripts/testfun/assert.m	Wed Dec 12 17:22:08 2018 -0800
+++ b/scripts/testfun/assert.m	Wed Dec 12 21:44:36 2018 -0500
@@ -88,7 +88,7 @@
           || isempty (cond) || ! all (cond(:)))
         if (nargin == 1)
           ## Perhaps, say which elements failed?
-          argin = ["(" inputname(1) ")"];
+          argin = ["(" inputname(1, false) ")"];
           error ("assert %s failed", argin);
         else
           error (varargin{:});
@@ -403,7 +403,7 @@
       if (! isempty (err.index))
         arg_names = cell (nargin, 1);
         for i = 1:nargin
-          arg_names{i} = inputname (i);
+          arg_names{i} = inputname (i, false);
         endfor
         argin = ["(" strjoin(arg_names, ",") ")"];
         if (! isempty (errmsg))
--- a/scripts/testfun/test.m	Wed Dec 12 17:22:08 2018 -0800
+++ b/scripts/testfun/test.m	Wed Dec 12 21:44:36 2018 -0500
@@ -815,7 +815,7 @@
 ## Create structure with fieldnames the name of the input variables.
 function s = var2struct (varargin)
   for i = 1:nargin
-    s.(inputname (i)) = varargin{i};
+    s.(inputname (i, true)) = varargin{i};
   endfor
 endfunction