CliWrap is a library that makes it easier to interact with command line interfaces. It provides a convenient wrapper around the target executable, allowing you to pass execution parameters and read the resulting output. The library can also handle errors reported by the underlying process, allows command cancellation and has both synchronous and asynchronous APIs.
- NuGet:
dotnet add package CliWrap
- Continuous integration
- Full abstraction over
System.Diagnostics.Process
- Execute commands in a synchronous, asynchronous, fire-and-forget manner
- Pass in command line arguments, standard input and environment variables
- Get process exit code, standard output and standard error as the result
- Abort the execution early using
System.Threading.CancellationToken
- Set up callbacks that trigger when a process writes to standard output or error
- Custom encoding settings for standard input, output and error
- Fluent interface
- Targets .NET Framework 4.5+ and .NET Standard 2.0+
- No external dependencies
var result = await Cli.Wrap("cli.exe")
.SetArguments("Hello world!")
.ExecuteAsync();
var exitCode = result.ExitCode;
var stdOut = result.StandardOutput;
var stdErr = result.StandardError;
var startTime = result.StartTime;
var exitTime = result.ExitTime;
var runTime = result.RunTime;
var result = await Cli.Wrap("cli.exe")
.SetStandardInput("Hello world from stdin!") // can also pipe a stream instead
.ExecuteAsync();
var result = await Cli.Wrap("cli.exe")
.SetEnvironmentVariable("var1", "value1")
.SetEnvironmentVariable("var2", "value2")
.ExecuteAsync();
using (var cts = new CancellationTokenSource())
{
cts.CancelAfter(TimeSpan.FromSeconds(5)); // e.g. timeout of 5 seconds
var result = await Cli.Wrap("cli.exe")
.SetCancellationToken(cts.Token)
.ExecuteAsync();
}
var result = await Cli.Wrap("cli.exe")
.SetStandardOutputCallback(l => Console.WriteLine($"StdOut> {l}")) // triggered on every line in stdout
.SetStandardErrorCallback(l => Console.WriteLine($"StdErr> {l}")) // triggered on every line in stderr
.ExecuteAsync();
var result = await Cli.Wrap("cli.exe")
.EnableExitCodeValidation(true) // throw exception on non-zero exit code (on by default)
.EnableStandardErrorValidation(true) // throw exception on non-empty stderr (off by default)
.ExecuteAsync();
If you really like my projects and want to support me, consider donating to me on Patreon or BuyMeACoffee. All donations are optional and are greatly appreciated. ๐