Exec 1.2.0

dotnet add package Exec --version 1.2.0                
NuGet\Install-Package Exec -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Exec" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Exec --version 1.2.0                
#r "nuget: Exec, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Exec as a Cake Addin
#addin nuget:?package=Exec&version=1.2.0

// Install Exec as a Cake Tool
#tool nuget:?package=Exec&version=1.2.0                

A dotnet library to fire subprocess command call.

master

Usage

Install from NuGet

dotnet add package Exec

This library supports .NET 5 and version onwards.

Basic usage

var output = await Exec.RunAsync("echo hello");
Assert.Contains("hello", output);

That's all.

Application just call the static methods of Exec class, along with below APIs.

Task<string> RunAsync(string command, CancellationToken cancellationToken = default)

Task<string> RunAsync(string command, ExecOption option, CancellationToken cancellationToken = default)

The ExecOption can accept customized parameters in case of the default behavior not meet your requirements.

Property Comment Default
Shell The shell application used to execute command passed in. In windows default to cmd.exe, and /bin/sh for Linux and macOS.
ShellParameter Shell parameter for carrying out the script file. In windows default to /q/ c, and empty string for Linxu and macOS.
ShellExtension Script file extension. In windows default to .bat, and .sh for Linxu and macOS.
TempFileLocation Directory for script file. User temp file directory.
Timeout Command execution timeout. Zero. Means no timeout applied.
OutputDataReceivedHandler Handler for new line of output data. Only works while IsStreamed=true. Discard the output data.
ErrorDataReceivedHandler Handler for new line of error data. Only works while IsStreamed=true. Discard the error data.
OnExitedHandler Handler for process exited. Do nothing.
OnCancelledHandler Handler for process was cancelled. Do nothing.
IsStreamed Switch for asynchronous output/error data handling. false

Advanced usage

Execute PowerShell command
var option = new ExecOption();
option.Shell = "pwsh";
option.ShellParameter = "";
option.ShellExtension = ".ps1";
var command = @"function Test-Add {
[CmdletBinding()]
param (
[int]$Val1,
[int]$Val2
)

Write-Output $($Val1 + $Val2)
}

Test-Add 10 12";
var output = await Exec.RunAsync(command, option);
Assert.Contains("22", output);
Execute command with timeout
var option = new ExecOption();
option.Timeout = TimeSpan.FromSeconds(3);
var sw = Stopwatch.StartNew();
var output = await Exec.RunAsync("timeout 60", option);
sw.Stop();
Assert.True(sw.Elapsed.TotalSeconds < 5);
Handler stdout and stderr asynchronously
var sb = new StringBuilder();
var option = new ExecOption();
option.IsStreamed = true;
option.OutputDataReceivedHandler = async (d) =>
{
    sb.AppendLine(d);
    await Task.CompletedTask;
};

var output = await Exec.RunAsync(@"echo hello
echo hello2
echo hello3", option);

Assert.True(output == "");

var sbStr = sb.ToString();
Assert.Contains("hello", sbStr);
Assert.Contains("hello2", sbStr);
Assert.Contains("hello3", sbStr);

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 366 11/24/2023
1.1.0 124 10/15/2023
1.0.0 126 10/12/2023
0.9.2 95 10/12/2023
0.9.1 92 10/11/2023
0.9.0 89 10/11/2023
0.1.0-alpha 79 10/10/2023
0.0.1-alpha 69 10/10/2023