
Set environment variables from a dictionary:.Sets additional environment variables exposed to the child process. WithWorkingDirectory("c:/projects/my project/") Sets the working directory of the child process.ĭefault: current working directory, i.e. In all other scenarios, both the array and builder overloads of WithArguments(.) offer a far more flexible alternative.
⚠️ String overload of WithArguments(.) only works well when the arguments are short, simple, and not expected to change.
Set arguments using a builder - same as above, but also works with non-string arguments and can be enhanced with your own extension methods:. Set arguments from a list - each element is treated as a separate argument and spaces are escaped automatically:. WithArguments("commit -m \"my commit\"") Sets the command line arguments passed to the child process. 💡 Command is an immutable object - all configuration methods listed here create a new instance instead of modifying the existing one. The fluent interface provided by the command object allows you to configure various options related to its execution.īelow list covers all available configuration methods and their usage. Programs can write arbitrary data (including binary) to output and error streams, which may be impractical to buffer in-memory.įor more advanced scenarios, CliWrap also provides other piping options, which are covered in the piping section. ⚠️ Be mindful when using ExecuteBufferedAsync(). implicitly configures pipes that write to in-memory buffers. Calling `ExecuteBufferedAsync()` instead of `ExecuteAsync()` In particular, the same thing shown above can also be achieved more succinctly with the ExecuteBufferedAsync() extension method: using CliWrap Handling command output is a very common use case, so CliWrap offers a few high-level execution models to make these scenarios simpler. This example command is configured to decode the data written to standard output and error streams as text, and append it to the corresponding StringBuilder buffers.Īfter the execution is complete, these buffers can be inspected to see what the process has printed to the console. Contains stdOut/stdErr buffered in-memory as string WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer)) WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer)) ⚠ This particular example can also be simplified with ExecuteBufferedAsync(). You can change this by calling WithStandardInputPipe(.), WithStandardOutputPipe(.), or WithStandardErrorPipe(.) to configure pipes for the corresponding streams: var stdOutBuffer = new StringBuilder() You can override this behavior by disabling result validation using WithValidation(CommandResultValidation.None).īy default, the process's standard input, output and error streams are routed to CliWrap's equivalent of the null device, which represents an empty source and a target that discards all data. ⚠️ CliWrap will throw an exception if the underlying process returns a non-zero exit code, as it usually indicates an error. #Clipwrap pc code#
The code above spawns a child process with the configured command line arguments and working directory, and then asynchronously waits for it to exit.Īfter the task has completed, it resolves to a CommandResult object that contains the process exit code and other related information. Var result = await Cli.Wrap("path/to/exe") Once the command is configured, you can run it by calling ExecuteAsync(): using CliWrap To build a command, start by calling Cli.Wrap(.) with the executable path, and then use the provided fluent interface to configure arguments, working directory, or other options. Similarly to a shell, CliWrap's base unit of work is a command - an object that encodes instructions for running a process. 📺 Watch Intro to CliWrap on YouTube for a deep look into the library and its features!
Provides safety against typical deadlock scenarios.Designed with strict immutability in mind.Fully asynchronous and cancellation-aware API.To learn more about the war and how you can help, click here. You reject false narratives perpetuated by Russian state propaganda.You support Ukraine's territorial integrity, including its claims over temporarily occupied territories of Crimea and Donbas.You recognize that Russia is an occupant that unlawfully invaded a sovereign state.You condemn Russia and its military aggression against Ukraine.Terms of useīy using this project or its source code, for any purpose and in any shape or form, you grant your implicit agreement to all the following statements: It provides a convenient model for launching processes, redirecting input and output streams, awaiting completion, handling cancellation, and more. CliWrap is a library for interacting with external command line interfaces.