std.process
Other programs, with their standard input, output and error. run() runs a program to its end and gives its exit code and what it printed; start() starts one and gives a Process, to write to its input and read its output while it runs.
A program is found as the shell finds it: in the folders of PATH when its name has no folder, and on Windows without .exe too. Its arguments reach it as they are, without a shell that interprets them. An exit code other than 0 is a value to check; a program that cannot start is an exception.
import std.process
process.Output result = process.run("git", ["status", "--short"])
if (result.code != 0) {
eprintln("git failed: {}", result.err)
}
print("{}", result.out)The guide: Files, processes and time.
Generata dalla documentazione nei sorgenti della libreria, che è in inglese.
#Tipi
#Output
struct OutputWhat run() gives: the exit code, and the standard output and error as text, with U+FFFD in place of the bytes that are not UTF-8.
i32 code: the exit code: 0 for success; on Linux, 128 + the signal that ended itstring out: the standard outputstring err: the standard error
#Process
struct ProcessA program that runs, from start(): this program writes to its standard input and reads its standard output, while its errors go where those of this program go. When the Process is destroyed, as at the end of its block, the input and the output are closed and the end of the program is waited for, unless wait() did it already.
process.Process p = process.start("git", ["hash-object", "--stdin"])
p.write("hello\n")
p.closeInput()
string hash = p.readAll() // "ce013625030ba8dba906f756967f9e9ca394464a\n"
i32 code = p.wait()#Process.write
void write(string text)Writes the text to the standard input of the program, and waits while the program does not read it. Throws if the input is closed, or if the program does not read it any more, as when it has ended.
#Process.writeBytes
void writeBytes(list<u8> bytes)Writes the bytes to the standard input of the program, as write() does.
#Process.closeInput
void closeInput()Closes the standard input of the program, which then sees its end: many programs, as sort, wait for it before they answer. A second call does nothing.
#Process.read
list<u8> read(i64 most)Up to most bytes of the standard output of the program, as soon as some arrive; empty at the end of the output.
#Process.readAll
string readAll()All the standard output of the program that is left, to its end, as text with U+FFFD in place of the bytes that are not UTF-8.
#Process.wait
i32 wait()Waits for the end of the program, and gives its exit code: on Linux, 128 + the signal that ended it, as the shells say. A second call gives the same code.
#Process.kill
void kill()Ends the program at once, if it is still running; wait() then gives its code.
#Funzioni
#run
Output run(string program, list<string> args)Runs a program to its end and gives its exit code, its output and its errors; its standard input is empty. An exit code other than 0 is a value to check; throws if the program cannot start, as when it does not exist.
#start
Process start(string program, list<string> args)Starts a program, which runs while this one writes to its input and reads its output. Its errors go where those of this program go. Throws if the program cannot start, as when it does not exist.
#status
i32 status(string program, list<string> args)Runs a program to its end with the standard input, output and error of this program, as a shell runs a command: who uses this program sees what it prints and answers what it asks. Its exit code; throws if the program cannot start, as when it does not exist.
i32 code = process.status("git", ["commit"])