loading...

Execute a Program in D

jessekphillips profile image Jesse Phillips Updated on ・1 min read

execute

Another helpful tool is to be able to run another program as part of a script. This is one area I find D handles really well and allows for similar controls to shell pipes.

For this toolbox I'll stick with the basic.

import std.process;
import std.exception;

auto res = execute(["app name", "arg 1", "arg 2"]);
enforce(res.status == 0, "App exited with failure");

import std.stdio;
writeln(res.output);

This method will wait for processing to complete and provide all the std output. spawnProcess can be used to allow program execution while the app runs, remember to wait before your program exits.

I like this approach because it is not subject strange escaping and quote rules from the shell. Though there is escapeShellCommand to help you.

Posted on by:

jessekphillips profile

Jesse Phillips

@jessekphillips

Senior Quality Assurance (SDET) ¶ Avid hobby D programmer ¶ Telling people what to do because I am right.

Discussion

pic
Editor guide