using System;
using System.Diagnostics;
namespace Unity.XR.XREAL.Editor
{
static class Cli
{
///
/// Executes a command line interface (CLI), capturing standard output and error. is
/// executed synchronously.
///
/// The path of the executable.
/// Additional arguments to pass to the executable.
/// A Tuple:
/// * string stdout: The standard output (stdout) resulting from the execution.
/// * string stderr: The standard error (stderr) resulting from the execution.
/// * int exitCode: The exit code resulting from the execution.
///
/// If there is no standard output or error, that return value may be the empty string (not `null`). Consider
/// using `string.IsNullOrEmpty(stderr)` to check for errors.
///
/// Thrown if is `null` or the empty
/// string.
public static (string stdout, string stderr, int exitCode) Execute(string fileName, string arguments)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentException($"{nameof(fileName)} must not be null or empty.", nameof(fileName));
var process = new Process
{
StartInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = fileName,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
},
EnableRaisingEvents = true
};
using (process)
{
process.Start();
var stdout = process.StandardOutput.ReadToEnd();
var stderr = process.StandardError.ReadToEnd();
process.WaitForExit();
return (stdout, stderr, process.ExitCode);
}
}
///
/// Executes a command line interface (CLI), capturing standard output and error. is
/// executed synchronously.
///
/// The path of the executable.
/// Additional arguments to pass to the executable.
/// A Tuple:
/// * string stdout: The standard output resulting from the execution.
/// * string stderr: The standard error resulting from the execution.
/// * int exitCode: The exit code resulting from the execution.
///
/// If there is no standard output or error, that return value may be the empty string (not `null`). Consider
/// using `string.IsNullOrEmpty(stderr)` to check for errors.
///
/// Thrown if is `null` or the empty
/// string.
public static (string stdout, string stderr, int exitCode) Execute(string fileName, string[] arguments = null) =>
Execute(fileName, arguments == null ? null : string.Join(" ", arguments));
}
}