Thread overview
"The D Way" to run a sequence of executables?
Aug 24, 2018
Matthew OConnor
Aug 24, 2018
Jonathan M Davis
Aug 24, 2018
Chris M.
Aug 25, 2018
JN
Aug 25, 2018
JN
Aug 28, 2018
Kagamin
August 24, 2018
I'd like to run a sequence of executables with something like std.process.execute, but I would like the sequence to error out if one of the executables returns a non-zero return code. What is the recommended way to do this? A wrapper that throws exceptions? Checking return values?
August 24, 2018
On Friday, August 24, 2018 11:36:25 AM MDT Matthew OConnor via Digitalmars- d-learn wrote:
> I'd like to run a sequence of executables with something like std.process.execute, but I would like the sequence to error out if one of the executables returns a non-zero return code. What is the recommended way to do this? A wrapper that throws exceptions? Checking return values?

AFAIK, you're only two options are to either make consequitive calls to one of the execute family of functions where you check the return code and act accordingly (whether you use exceptions to deal with it is up to you, but either way, you have to check the result of each call to execute, since it doesn't throw), or you use one of the functions which uses the shell (e.g. executeShell) and write it out the way you would on the command-line with ||'s and/or &&'s.

Either way, Phobos doesn't provide any functions that are specifically for calling a sequence of executables rather than just one. So, you're going to have to figure out how to put that together in a way that works best for you and what you're doing given what std.process has.

- Jonathan M Davis



August 24, 2018
On Friday, 24 August 2018 at 17:36:25 UTC, Matthew OConnor wrote:
> I'd like to run a sequence of executables with something like std.process.execute, but I would like the sequence to error out if one of the executables returns a non-zero return code. What is the recommended way to do this? A wrapper that throws exceptions? Checking return values?

Here'd be a neat way if you don't mind it not terminating early. The first call will need a dummy value for prevStatus where the status field is 0.

import std.typecons;

alias ExecuteTuple = Tuple!(int,"status",string,"output");

ExecuteTuple call(ExecuteTuple prevStatus, string process)
{
    import std.process;
    if (prevStatus.status != 0)
        return prevStatus;
    else
        return execute(process);
}
August 25, 2018
On Friday, 24 August 2018 at 17:36:25 UTC, Matthew OConnor wrote:
> I'd like to run a sequence of executables with something like std.process.execute, but I would like the sequence to error out if one of the executables returns a non-zero return code. What is the recommended way to do this? A wrapper that throws exceptions? Checking return values?

It's kind of a dirty and not very portable solution, but if you run by executeShell, you could so something like executeShell("cmd1 && cmd2 && cmd3") and let the shell do the sequence for you.
August 25, 2018
On Saturday, 25 August 2018 at 22:00:47 UTC, JN wrote:
> It's kind of a dirty and not very portable solution, but if you run by executeShell, you could so something like executeShell("cmd1 && cmd2 && cmd3") and let the shell do the sequence for you.

Oops, didn't notice it was suggested already.
August 28, 2018
Maybe http://libpipeline.nongnu.org/ can be an inspiration.