Mercurial > octave
changeset 28103:1cb3c33f97dc
copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
* copyfile.m: New internal variable "sts" to hold status of function.
When internal checks fail, check nargout == 0, and throw an error. Otherwise,
set output variables "status", "msg", "msgid" and immediately return from
function. Att end of function, based on nargout variable, either throw an
error if operation failed (sts = 0), or set output "status" to value of "sts"
and return information in outputs.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 18 Feb 2020 17:22:40 -0800 |
parents | 4d021e0dcfee |
children | b2e0a2ddfd7d |
files | scripts/miscellaneous/copyfile.m |
diffstat | 1 files changed, 28 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/miscellaneous/copyfile.m Tue Feb 18 16:46:15 2020 -0800 +++ b/scripts/miscellaneous/copyfile.m Tue Feb 18 17:22:40 2020 -0800 @@ -53,7 +53,7 @@ endif max_cmd_line = 1024; - status = true; + sts = 1; msg = ""; msgid = ""; @@ -87,13 +87,27 @@ ## If f1 has more than 1 element then f2 must be a directory isdir = isfolder (f2); if (numel (f1) > 1 && ! isdir) - error ("copyfile: when copying multiple files, F2 must be a directory"); + if (nargout == 0) + error ("copyfile: when copying multiple files, F2 must be a directory"); + else + status = 0; + msg = "when copying multiple files, F2 must be a directory"; + msgid = "copyfile"; + return; + endif endif ## Protect the filename(s). f1 = glob (f1); if (isempty (f1)) - error ("copyfile: no files to move"); + if (nargout == 0) + error ("copyfile: no files to move"); + else + status = 0; + msg = "no files to move"; + msgid = "copyfile"; + return; + endif endif p1 = sprintf ('"%s" ', f1{:}); p2 = tilde_expand (f2); @@ -118,7 +132,7 @@ ## Copy the files. [err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2)); if (err != 0) - status = false; + sts = 0; msgid = "copyfile"; break; endif @@ -133,11 +147,19 @@ ## Copy the files. [err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2)); if (err != 0) - status = false; + sts = 0; msgid = "copyfile"; endif endif + if (nargout == 0) + if (sts == 0) + error ("copyfile: operation failed: %s", msg); + endif + else + status = sts; + endif + endfunction @@ -172,3 +194,4 @@ %!error <F1 must be a string> copyfile (1, "foobar") %!error <F2 must be a string> copyfile ("foobar", 1) %!error <F2 must be a directory> copyfile ({"a", "b"}, "%_NOT_A_DIR_%") +%!error <no files to move> copyfile ("%_NOT_A_FILENAME1_%", "%_NOT_A_FILENAME2_%")