DEV Community

Jesse Phillips
Jesse Phillips

Posted on • Edited on

4 1

Execute a Program in D

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);
Enter fullscreen mode Exit fullscreen mode

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay