How to use CliWrap to make the command line in C# interact
Editor to share with you how to use CliWrap to let the command line in C# interact. I hope you will get something after reading this article. Let's discuss it together.
Command-line interaction in code is a common scenario, especially in some CI CD automation processes, where we used System.Diagnostics.Process API before, and now there is a more flexible tool, CliWarp, a command-line interaction library used on the .NET platform, which makes command-line interaction light by using Fluent's API in C #.
Https://github.com/Tyrrrz/CliWrap
The main features are as follows:
Based on System.Diagnostics.Process
Simple, smooth API design
Flexible support for pipeline mode
Secure asynchronism and support for cancellation API
Cross-platform, available on Windows, Linux, and macOS
Supports .NET Standard 2.0 +, .NET Core 3.0 +, .NET Framework 4.6.1 +
Similar to shell, the basic unit of work of CliWrap is a command, which first executes Cli.Wrap (...). Create a command with the parameter as the path to the executable file, then configure it through fluent api, and finally call the ExecuteAsync run command, as follows:
Using CliWrap;using CliWrap.Buffered;var result = await Cli.Wrap ("path/to/exe") / /. .ExecuteBufferedAsync ()
Configuration parameters
Var cmd = Cli.Wrap ("git") .WithArguments ("commit-m\" my commit\ ""); var cmd = Cli.Wrap ("git") .WithArguments (new [] {"commit", "- m", "my commit"}) Var cmd = Cli.Wrap ("git") .WithArguments (args = > args .add ("clone") .add ("https://github.com/Tyrrrz/CliWrap") .add ("-- depth ") .Add (20))
Configure the working directory
The default is the current directory, and you can also specify the relative and absolute paths of the folder
Var cmd = Cli.Wrap ("git") .WithWorkingDirectory ("c:/projects/my project/")
Configure environment variables
Var cmd = Cli.Wrap ("git") .WithEnvironmental variables (env = > env .Set ("GIT_AUTHOR_NAME", "John") .Set ("GIT_AUTHOR_EMAIL", "john@email.com"))
Timeout and cancellation
Using var cts = new CancellationTokenSource (); cts.CancelAfter (TimeSpan.FromSeconds (10)); var result = await Cli.Wrap ("path/to/exe") .ExecuteAsync (cts.Token)
Event flow based on pull
In addition to executing commands, CliWrap also supports an event flow model that allows you to subscribe to related event callbacks.
StartedCommandEvent-received only once, when the command starts execution (including process ID)
StandardOutputCommandEvent-received each time the underlying process writes a new line to the output stream (including text as a string)
StandardErrorCommandEvent-received each time the underlying process writes a new line to the error stream (including text as a string)
ExitedCommandEvent-received only once, when the command finishes execution (including exit code)
Using CliWrap;using CliWrap.EventStream;var cmd = Cli.Wrap ("foo"). WithArguments ("bar"); await foreach (var cmdEvent in cmd.ListenAsync ()) {switch (cmdEvent) {case StartedCommandEvent started: _ output.WriteLine ($"Process started; ID: {started.ProcessId}"); break; case StandardOutputCommandEvent stdOut: _ output.WriteLine ($"Out > {stdOut.Text}"); break Case StandardErrorCommandEvent stdErr: _ output.WriteLine ($"Err > {stdErr.Text}"); break; case ExitedCommandEvent exited: _ output.WriteLine ($"Process exited; Code: {exited.ExitCode}"); break;}}
The output is as follows:
After reading this article, I believe you have a certain understanding of "how to use CliWrap to enable command line interaction in C#". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!