AlastairLundy.CliInvoke.Core 2.0.0-beta.2

Prefix Reserved
This is a prerelease version of AlastairLundy.CliInvoke.Core.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package AlastairLundy.CliInvoke.Core --version 2.0.0-beta.2
                    
NuGet\Install-Package AlastairLundy.CliInvoke.Core -Version 2.0.0-beta.2
                    
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="AlastairLundy.CliInvoke.Core" Version="2.0.0-beta.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AlastairLundy.CliInvoke.Core" Version="2.0.0-beta.2" />
                    
Directory.Packages.props
<PackageReference Include="AlastairLundy.CliInvoke.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AlastairLundy.CliInvoke.Core --version 2.0.0-beta.2
                    
#r "nuget: AlastairLundy.CliInvoke.Core, 2.0.0-beta.2"
                    
#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.
#:package AlastairLundy.CliInvoke.Core@2.0.0-beta.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AlastairLundy.CliInvoke.Core&version=2.0.0-beta.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=AlastairLundy.CliInvoke.Core&version=2.0.0-beta.2&prerelease
                    
Install as a Cake Tool

CliInvoke.Core

This package contains Process Running and handling abstractions as well as common types used by implementing classes.

For an implementing package, check out CliInvoke.

Key Abstractions:

  • IProcessInvoker

  • IProcessConfigurationInvoker

  • Piping:

    • IProcessPipeHandler
  • Fluent Builders:

    • IArgumentsBuilder - An interface to assist with Argument Building and argument escaping.
    • IEnvironmentVariablesBuilder - An interface to assist with setting Environment variables.
    • IProcessConfigurationBuilder - An interface to fluently configure and build ProcessConfiguration objects.
    • IProcessExitConfigurationBuilder - An interface to fluently configure and build ProcessExitConfiguration objects.
    • IProcessResourcePolicyBuilder - An interface to fluently configure and build ProcessResourcePolicy objects.
    • IProcessTimeoutPolicyBuilder
    • IUserCredentialBuilder

NuGet NuGet

Table of Contents

Features

  • Easy to use safe Process Running classes and interfaces
  • Models that help abstract away some of the complicated nature of Process objects
  • SourceLink support

How to install and use CliInvoke.Core

CliInvoke.Core is available on the Nuget Gallery here.

Installing CliInvoke.Core

CliInvoke.Core packages can be installed via the .NET SDK CLI, Nuget via your IDE or code editor's package interface, or via the Nuget website.

Package Name Nuget Link .NET SDK CLI command
AlastairLundy.CliInvoke.Core AlastairLundy.CliInvoke.Core Nuget dotnet add package AlastairLundy.CliInvoke.Core

Supported Platforms

CliInvoke.Core can be added to any .NET Standard 2.0, .NET Standard 2.1, .NET 8, or .NET 9 supported project.

The following table details which target platforms are supported for running Processes.

Operating System/Platform specific TFM Support Status Notes
Windows Fully Supported ✅
macOS Fully Supported ✅
Mac Catalyst Untested Platform ⚠️ Support for this platform has not been tested but should theoretically work.
Linux Fully Supported ✅
FreeBSD Fully Supported ✅
Android Untested Platform ⚠️ Support for this platform has not been tested but should theoretically work.
IOS Not Supported ❌ Not supported due to Process.Start() not supporting IOS. ^2
tvOS Not Supported ❌ Not supported due to Process.Start() not supporting tvOS ^2
watchOS Not Supported ❌ Not supported due to Process.Start() not supporting watchOS ^3
Browser Not Planned ❌ Not planned due to Client Side Rendering not being a valid target Platform for executing processes.

^2 - See the Process class documentation for more info.

^3 - lack of IOS support implies lack of watchOS support since watchOS is based on IOS.

Note: This library has not been tested on Android or Tizen.

Using CliInvoke / Examples

The two main use cases for CliInvoke are intended to be:

  1. executing Programs/Commands programatically which involves using abstractions to improve the experience of external Program/Command running.
  2. safe Process Running which involves avoiding the pitfalls of the Process class whilst still dealing with ProcessStartInfo.

CliInvoke provides both options to give developers a choice in the approach they adopt. They are both equally safe and valid.

Approaches

Safe Process Running

CliInvoke offers safe abstractions around Process Running to avoid accidentally not disposing of Processes, along with avoiding other pitfalls.

IProcessInvoker and IProcessConfigurationInvoker are both equally capable of fulfilling this criterion, however IProcessInvoker works with ProcessStartInfo objects and IProcessConfigurationInvoker works with ProcessConfiguration objects.

If you don't want to use CliInvoke's abstractions around Processes, such as ProcessConfiguration and CliInvoke's other primitives, then IProcessInvoker is a better fit.

Note: Neither IProcessInvoker nor IProcessConfigurationInvoker are dependent upon on the other to work.

IProcessInvoker

IProcessInvoker is an interface for creating, running, and safely disposing of Processes based on ProcessStartInfo objects.

The ExecuteAsync, ExecuteBufferedExitAsync, ExecutePipedExitAsync methods provide for:

  1. process creation
  2. safe process running (including process disposal, even in the case of an Exception)
  3. gathering the results of the Process's execution (varies depending on the specific method)
  4. disposing of the Process after it has exited
  5. returning the gathered Process execution results

These examples show how they might be used:

Running Programs/Commands

Because of how much of a minefield the Process class is and how difficult it can be to configure correctly, CliInvoke provides some abstractions to make it easier to configure Programs/Commands to be run.

CliInvoke provides fluent builder interfaces and implementing classes to easily configure ProcessConfiguration. ProcessConfiguration is CliInvoke's main form of Process configuration (hence the name).

The use of ProcessConfiguration can be avoided if you want to stick with ProcessStartInfo for configuration, but this means IProcessInvoker is the appropriate interface to use for creating and executing Processes.

Approach Examples

IProcessInvoker
ExecuteAsync
using AlastairLundy.CliInvoke.Core;
using AlastairLundy.CliInvoke.Core.Primitives;

// Using namespaces for Dependency Injection code ommitted for clarity

      // Dependency Injection setup code ommitted for clarity

    IProcessInvoker _processInvoker = serviceProvider.GetRequiredService<IProcessInvoker>();
    
    // Define processStartInfo here
    
    // This process is created, executed, disposed of, and the results returned.
    ProcessResult process = await _processInvoker.ExecuteAsync(processStartInfo, ProcessResourcePolicy.Default,
        ProcessTimeoutPolicy.None, ProcessResultValidation.ExitCodeZero);
ExecuteBufferedAsync
using AlastairLundy.CliInvoke.Core;
using AlastairLundy.CliInvoke.Core.Primitives;

// Using namespaces for Dependency Injection code ommitted for clarity

      // Dependency Injection setup code ommitted for clarity

    IProcessInvoker _processInvoker = serviceProvider.GetRequiredService<IProcessInvoker>();
    
    // Define processStartInfo here
    
    // This process is created, executed, disposed of, and the results returned.
    BufferedProcessResult process = await _processInvoker.ExecuteBufferedAsync(processStartInfo, ProcessResourcePolicy.Default,
        ProcessTimeoutPolicy.None, ProcessResultValidation.ExitCodeZero);
IProcessConfigurationInvoker

The following examples shows how to configure and build a ProcessConfiguration depending on whether Buffering the output is desired.

Non-Buffered Execution Example

This example gets a non buffered ProcessResult that contains basic process exit code, id, and other information.

using AlastairLundy.CliInvoke;
using AlastairLundy.CliInvoke.Core;

using AlastairLundy.CliInvoke.Builders;
using AlastairLundy.CliInvoke.Core.Builders;

using AlastairLundy.CliInvoke.Core.Primitives;

  //Namespace and class code ommitted for clarity 

  // ServiceProvider and Dependency Injection setup code ommitted for clarity
  
  IProcessConfigurationInvoker _processConfigInvoker = serviceProvider.GetRequiredService<IProcessConfigurationInvoker>();

  // Fluently configure your Command.
  IProcessConfigurationBuilder builder = new ProcessConfigurationBuilder("Path/To/Executable")
                            .WithArguments(["arg1", "arg2"])
                            .WithWorkingDirectory("/Path/To/Directory");
  
  // Build it as a ProcessConfiguration object when you're ready to use it.
  ProcessConfiguration config = builder.Build();
  
  // Execute the process through ProcessInvoker and get the results.
ProcessResult result = await _processConfigInvoker.ExecuteAsync(config);
Buffered Execution Example

This example gets a BufferedProcessResult which contains redirected StandardOutput and StandardError as strings.

using AlastairLundy.CliInvoke;
using AlastairLundy.CliInvoke.Core;

using AlastairLundy.CliInvoke.Builders;
using AlastairLundy.CliInvoke.Core.Builders;

using AlastairLundy.CliInvoke.Core.Primitives;

  //Namespace and class code ommitted for clarity 

  // ServiceProvider and Dependency Injection setup code ommitted for clarity
  
  IProcessConfigurationInvoker _processConfigInvoker = serviceProvider.GetRequiredService<IProcessConfigurationInvoker>();

  // Fluently configure your Command.
  IProcessConfigurationBuilder builder = new ProcessConfigurationBuilder("Path/To/Executable")
                            .WithArguments(["arg1", "arg2"])
                            .WithWorkingDirectory("/Path/To/Directory");
  
  // Build it as a ProcessConfiguration object when you're ready to use it.
  ProcessConfiguration config = builder.Build();
  
  // Execute the process through ProcessInvoker and get the results.
BufferedProcessResult result = await _processConfigInvoker.ExecuteBufferedAsync(config);

Command/Program Execution

How to Contribute

Thank you in advance for considering contributing to CliInvoke.

Please see the CONTRIBUTING.md file for code and localization contributions.

If you want to file a bug report or suggest a potential feature to add, please check out the GitHub issues page to see if a similar or identical issue is already open. If there is not already a relevant issue filed, please file one here and follow the respective guidance from the appropriate issue template.

Thanks.

License

CliInvoke.Core is licensed under the MPL 2.0 license. If you modify any of CliInvoke.Core's files, then the modified files must be licensed under the MPL 2.0.

If you use CliInvoke.Core in your project, please make an exact copy of the contents of CliInvoke.Core's LICENSE.txt file available either in your third party licenses txt file or as a separate txt file.

Acknowledgements

Projects

This project would like to thank the following projects for their work:

  • Microsoft.Bcl.HashCode for providing a backport of the HashCode class and static methods to .NET Standard 2.0

For more information, please see the THIRD_PARTY_NOTICES file.

Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on AlastairLundy.CliInvoke.Core:

Package Downloads
AlastairLundy.CliInvoke

CliInvoke is a .NET Library for interacting with Command Line Interfaces, CliInvoke is a library for interacting with Command Line Interfaces and wrapping around executables.

AlastairLundy.CliInvoke.Extensibility

Makes extending CliInvoke, and using custom Command Runners and Command Configurations easier.

AlastairLundy.CliInvoke.Extensions

Adds a ``AddCliInvoke`` Dependency Injection extension method to enable easy CliInvoke setup when using the Microsoft.Extensions.DependencyInjection package.

AlastairLundy.CliInvoke.Specializations

CliRunner Specializations is a library for providing Specializations of CliRunner's Commands.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0-beta.3 68 10/5/2025
2.0.0-beta.2 192 10/1/2025
2.0.0-beta.1 185 9/27/2025
2.0.0-alpha.8 205 9/25/2025
2.0.0-alpha.7 329 9/15/2025
2.0.0-alpha.6 196 8/25/2025
2.0.0-alpha.5 256 8/7/2025
2.0.0-alpha.4 168 8/4/2025
2.0.0-alpha.3 68 7/19/2025
2.0.0-alpha.2 171 7/10/2025
2.0.0-alpha.1 141 6/18/2025
1.6.0 205 10/1/2025
1.5.2 497 9/16/2025
1.5.1 409 8/25/2025
1.5.0 230 7/30/2025
1.4.5 339 7/10/2025
1.4.4 353 7/7/2025
1.4.3 371 6/23/2025
1.4.2 583 6/11/2025
1.4.1 254 6/2/2025
1.4.0 205 5/27/2025
1.3.1 624 5/19/2025
1.3.0 168 4/26/2025

## 2.0.0 Beta 2

### Core package only
* Removed dependency on DotExtensions
* Moved extension methods to CliInvoke main package
* Deprecated some extension methods and constructors in classes for removal in v2
* Renamed the existing ``IProcessInvoker`` interface to ``IProcessConfigurationInvoker``
* Created a new ``IProcessInvoker`` that deals with ``ProcessStartInfo`` objects rather than ``ProcessConfiguration`` objects
* Removed ``IProcessFactory``