1 using System;
2 using System.Threading;
3
4 public int Run(const string& command, string& errorLines)
5 {
6 Process.Redirections redirections = cast<Process.Redirections>(Process.Redirections.processStdOut | Process.Redirections.processStdErr);
7 Process process(command, redirections);
8 while (!process.Eof(Process.StdHandle.stdOut))
9 {
10 string line = process.ReadLine(Process.StdHandle.stdOut);
11 Console.WriteLine(line);
12 }
13 if (!process.Eof(Process.StdHandle.stdErr))
14 {
15 errorLines = process.ReadToEnd(Process.StdHandle.stdErr);
16 }
17 process.WaitForExit();
18 int exitCode = process.ExitCode();
19 return exitCode;
20 }
21
22 int main(int argc, const char** argv)
23 {
24 try
25 {
26 TimePoint start = Now();
27 string command;
28 string errors;
29 for (int i = 1; i < argc; ++i;)
30 {
31 string arg = argv[i];
32 if (!command.IsEmpty())
33 {
34 command.Append(' ');
35 }
36 command.Append(arg);
37 }
38 int exitCode = Run(command, errors);
39 if (exitCode != 0)
40 {
41 throw Exception("executing command '" + command + "' failed with exit code " + ToString(exitCode) + ": " + errors);
42 }
43 TimePoint end = Now();
44 Duration dur = end - start;
45 Console.Out() << "executing command '" + command << "' took " << dur.Milliseconds() << " ms" << endl();
46 Console.Out() << "command returned exit code: " << exitCode << endl();
47 }
48 catch (const Exception& ex)
49 {
50 Console.Error() << ex.Message() << endl();
51 return 1;
52 }
53 return 0;
54 }