AlastairLundy.CliInvoke
2.0.0-beta.2
Prefix Reserved
See the version list below for details.
dotnet add package AlastairLundy.CliInvoke --version 2.0.0-beta.2
NuGet\Install-Package AlastairLundy.CliInvoke -Version 2.0.0-beta.2
<PackageReference Include="AlastairLundy.CliInvoke" Version="2.0.0-beta.2" />
<PackageVersion Include="AlastairLundy.CliInvoke" Version="2.0.0-beta.2" />
<PackageReference Include="AlastairLundy.CliInvoke" />
paket add AlastairLundy.CliInvoke --version 2.0.0-beta.2
#r "nuget: AlastairLundy.CliInvoke, 2.0.0-beta.2"
#:package AlastairLundy.CliInvoke@2.0.0-beta.2
#addin nuget:?package=AlastairLundy.CliInvoke&version=2.0.0-beta.2&prerelease
#tool nuget:?package=AlastairLundy.CliInvoke&version=2.0.0-beta.2&prerelease
CliInvoke
CliInvoke is a .NET library for interacting with Command Line Interfaces and wrapping around executables.
Launch processes, redirect standard input and output streams, await process completion and so much more.
<img src="https://github.com/alastairlundy/CliInvoke/blob/main/.assets/icon.png" width="192" height="192" alt="CliInvoke Logo">
Table of Contents
- Features
- Why CliInvoke?
- Installing CliInvoke
- CliInvoke Examples
- Contributing to CliInvoke
- Roadmap
- License
- Acknowledgements
Features
- Promotes the single responsibility principle and separation of concerns
- Supports .NET 8 and newer TFMs, and has few dependencies.
- Dependency Injection extensions to make using CliInvoke a breeze.
- Support for specific specializations such as running executables or commands via Windows Powershell or CMD on Windows <sup>1</sup>
- SourceLink support
<sup>1</sup> - The Specialization library is distributed separately here.
Why use CliInvoke over CliWrap?
- Greater separation of concerns - Command Building, Command Running, and Command Pipe handling are moved to separate classes.
- Supports Dependency Injection
- Classes and code follow the Single Responsibility Principle
- No hidden or additional licensing terms are required beyond the source code license.
- No imported C code - This library is entirely written in C#.
- No lock in regarding Piping support
- Uses .NET's built in
Process
type.
How to install and use CliInvoke
CliInvoke is available on Nuget.
These are the CliInvoke projects:
- CliInvoke - The main CliInvoke package.
- CliInvoke.Extensions
- CliInvoke.Specializations
Installing CliInvoke
CliInvoke's 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 | CliInvoke Nuget | dotnet add package AlastairLundy.CliInvoke |
AlastairLundy.CliInvoke.Extensions | AlastairLundy.CliInvoke.Extensions Nuget | dotnet add package AlastairLundy.CliInvoke.Extensions |
AlastairLundy.CliInvoke.Specializations | AlastairLundy.CliInvoke.Specializations Nuget | dotnet add package AlastairLundy.CliInvoke.Specializations |
Supported Platforms
CliInvoke can currently be added to .NET 8, or .NET 9 or newer supported projects.
The following table details which target platforms are supported for executing commands via CliInvoke.
Operating System | 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. <sup>3</sup> |
tvOS | Not Supported ❌ | Not supported due to Process.Start() not supporting tvOS <sup>3</sup> |
watchOS | Not Supported ❌ | Not supported due to Process.Start() not supporting watchOS <sup>4</sup> |
Browser | Not Planned ❌ | Not supported due to not being a valid target Platform for executing programs or processes. |
<sup>3</sup> - See the Process class documentation for more info.
<sup>4</sup> - Lack of watchOS support is implied by lack of IOS 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:
- executing Programs/Commands programatically which involves using abstractions to improve the experience of external Program/Command running.
- safe Process Running which involves avoiding the pitfalls of the
Process
class whilst still dealing withProcessStartInfo
.
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:
- process creation
- safe process running (including process disposal, even in the case of an
Exception
) - gathering the results of the Process's execution (varies depending on the specific method)
- disposing of the Process after it has exited
- 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);
How to Build CliInvoke's code
Requirements
CliInvoke requires the latest .NET release SDK to be installed to target all supported TFM (Target Framework Moniker) build targets.
Currently, the required .NET SDK is .NET 9.
The current build targets include:
- .NET 8
- .NET 9
- .NET Standard 2.0 (for CliInvoke 1.x only)
Any version of the .NET 9 SDK can be used, but using the latest version is preferred.
Versioning new releases
CliInvoke aims to follow Semantic versioning with [Major].[Minor].[Build]
for most circumstances and an optional .[Revision]
when only a configuration change is made, or a new build of a preview release is made.
Pre-releases
Pre-release versions should have a suffix of -alpha, -beta, -rc, or -preview followed by a .
and what pre-release version number they are. The number should be incremented by 1 after each release unless it only contains a configuration change, or another packaging, or build change. An example pre-release version may look like 1.1.0-alpha.2 , this version string would indicate it is the 2nd alpha pre-release version of 1.1.0 .
Stable Releases
Stable versions should follow semantic versioning and should only increment the Revision number if a release only contains configuration or build packaging changes, with no change in functionality, features, or even bug or security fixes.
Releases that only implement bug fixes should see the Build version incremented.
Releases that add new non-breaking changes should increment the Minor version. Minor breaking changes may be permitted in Minor version releases where doing so is necessary to maintain compatibility with an existing supported platform, or an existing piece of code that requires a breaking change to continue to function as intended.
Releases that add major breaking changes or significantly affect the API should increment the Major version. Major version releases should not be released with excessive frequency and should be released when there is a genuine need for the API to change significantly for the improvement of the project.
Building for Testing
You can build for testing by building the desired project within your IDE or VS Code, or manually by entering the following command: dotnet build -c Debug
.
If you encounter any bugs or issues, try running the CliInvoke.Tests
project and setting breakpoints in the affected CliInvoke project's code where appropriate. Failing that, please report the issue if one doesn't already exist for the bug(s).
Building for Release
Before building a release build, ensure you apply the relevant changes to the relevant .csproj
file corresponding to the package you are trying to build:
- Update the Package Version variable
- Update the project file's Changelog
- Remove/replace the CliInvoke icon if distributing a non-official release build to a wider audience. See CliInvoke Assets for more details.
You should ensure the project builds under debug settings before producing a release build.
Producing Release Builds
To manually build a project for release, enter dotnet build -c Release /p:ContinuousIntegrationBuild=true
for a release with SourceLink enabled or just dotnet build -c Release
for a build without SourceLink.
Builds should generally always include Source Link and symbol packages if intended for wider distribution.
NOTES:
CliInvoke.Specializations
andCliInvoke.Extensions
both take a dependency on the CliInvoke base package from Nuget - For the respective libraries to use a newer CliInvoke version, that version must be published on Nuget.
How to Contribute to CliInvoke
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.
CliInvoke's Roadmap
CliInvoke aims to make working with Commands and external processes easier.
Whilst an initial set of features are available in version 1, there is room for more features, and for modifications of existing features in future updates.
That being said, all stable releases from 1.0 onwards must be stable and should not contain regressions.
Future updates should aim focus on one or more of the following:
- Improved ease of use
- Improved stability
- New features
- Enhancing existing features
License
CliInvoke is licensed under the MPL 2.0 license. If you modify any of CliInvoke's files then the modified files must be licensed under the MPL 2.0 .
If you use CliInvoke in your project please make an exact copy of the contents of CliInvoke's LICENSE.txt file available either in your third party licenses txt file or as a separate txt file.
CliInvoke Assets
CliInvoke's Icon is NOT licensed under the MPL 2.0 license and is licensed under Copyright with all rights reserved to me (Alastair Lundy).
If you fork CliInvoke and re-distribute it, please replace the usage of the icon unless you have prior written agreements from me.
Acknowledgements
Projects
This project would like to thank the following projects for their work:
For more information, please see the THIRD_PARTY_NOTICES file.
Product | Versions 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. |
-
net8.0
- AlastairLundy.CliInvoke.Core (>= 2.0.0-beta.2)
- AlastairLundy.DotExtensions (>= 8.5.1)
-
net9.0
- AlastairLundy.CliInvoke.Core (>= 2.0.0-beta.2)
- AlastairLundy.DotExtensions (>= 8.5.1)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on AlastairLundy.CliInvoke:
Package | Downloads |
---|---|
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.WCountLib.Providers.wc
This package provides implementations for WCountLib.Abstractions interfaces that use Posix's and Unix's ``wc`` program to perform the calculations. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
2.0.0-beta.3 | 30 | 10/5/2025 |
2.0.0-beta.2 | 155 | 10/1/2025 |
2.0.0-beta.1 | 140 | 9/27/2025 |
2.0.0-alpha.8 | 164 | 9/25/2025 |
2.0.0-alpha.7 | 312 | 9/15/2025 |
2.0.0-alpha.6 | 181 | 8/25/2025 |
2.0.0-alpha.5 | 238 | 8/7/2025 |
2.0.0-alpha.4 | 160 | 8/4/2025 |
2.0.0-alpha.3 | 56 | 7/19/2025 |
2.0.0-alpha.2 | 150 | 7/10/2025 |
2.0.0-alpha.1 | 135 | 6/18/2025 |
1.6.0 | 135 | 10/1/2025 |
1.5.2 | 439 | 9/16/2025 |
1.5.1 | 357 | 8/25/2025 |
1.5.0 | 180 | 7/30/2025 |
1.4.5 | 280 | 7/10/2025 |
1.4.4 | 279 | 7/7/2025 |
1.4.3 | 255 | 6/23/2025 |
1.4.1 | 558 | 6/11/2025 |
1.4.0 | 273 | 6/9/2025 |
1.3.1 | 317 | 5/23/2025 |
1.2.0 | 510 | 4/8/2025 |
1.1.0 | 332 | 3/18/2025 |
1.0.0 | 401 | 3/2/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``
### Main package only
* Updated DotExtensions from 8.5.0 to 8.5.1
* Added some Process related extension methods from DotExtensions back into CliInvoke
* Renamed the existing ``ProcessInvoker`` interface to ``ProcessConfigurationInvoker``
* Created a new ``ProcessInvoker`` that deals with ``ProcessStartInfo`` objects rather than ``ProcessConfiguration`` objects
* Removed ``ProcessFactory``