CliRunner 1.0.0-beta.1

This is a prerelease version of CliRunner.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package CliRunner --version 1.0.0-beta.1                
NuGet\Install-Package CliRunner -Version 1.0.0-beta.1                
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="CliRunner" Version="1.0.0-beta.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CliRunner --version 1.0.0-beta.1                
#r "nuget: CliRunner, 1.0.0-beta.1"                
#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 CliRunner as a Cake Addin
#addin nuget:?package=CliRunner&version=1.0.0-beta.1&prerelease

// Install CliRunner as a Cake Tool
#tool nuget:?package=CliRunner&version=1.0.0-beta.1&prerelease                

CliRunner

CliRunner is a library for interacting with Command Line Interfaces and wrapping around executables.

<img src="https://github.com/alastairlundy/CliRunner/blob/main/.assets/icon.png" width="192" height="192" alt="CliRunner Logo">

NuGet NuGet

Features

  • Promotes the single responsibility principle and separation of concerns
  • For .NET 8 and newer TFMs CliRunner has few dependencies.
  • Compatible with .NET Standard 2.0 and 2.1 ^1
  • Dependency Injection extensions to make using it easier.
  • Support for specific specializations such as running executables or commands via Windows Powershell or CMD on Windows ^2
  • SourceLink support

^1 - Polyfill is a dependency only required for .NET Standard 2.0 and 2.1 users. Microsoft.Bcl.HashCode is a dependency only required for .NET Standard 2.0 users.

^2 - The Specialization library is distributed separately.

Why use CliRunner over CliWrap?

  • Greater separation of concerns with the Command class - Command Building, Command Running, andCommand 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 - Use .NET's StreamWriter and StreamReader classes as inputs and outputs respectively.
  • Uses .NET's built in Process type.

Supported Platforms

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

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. ^3
tvOS Not Supported ❌ Not supported due to Process.Start() not supporting tvOS ^3
watchOS Not Supported ❌ Not supported due to Process.Start() not supporting watchOS ^4
Browser Not Supported and Not Planned ❌ Not supported due to not being a valid target Platform for executing programs or processes.

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

^4 - 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!

Installation

Usage

Main Use Cases

One of the main use cases for CliRunner is intended to be executing programs programatically, but other valid use cases also exist such as safer Process Running.

Executing Commands

CliRunner enables use of a fluent builder style of syntax to easily configure and run Commands.

The following example shows how to configure and build a Command that returns a BufferedProcessResult which contains redirected StandardOutput and StandardError as strings.

using CliRunner;
using CliRunner.Builders.Abstractions;
using CliRunner.Builders;

  //Namespace and classs code ommitted for clarity 

  // ServiceProvider and Dependency Injection code ommitted for clarity
  
  ICommandRunner _commandRunner = serviceProvider.GetRequiredService<ICommandRunner>();

  // Fluently configure your Command.
  ICommandBuilder builder = new CommandBuilder("Path/To/Executable")
                            .WithArguments(["arg1", "arg2"])
                            .WithWorkingDirectory("/Path/To/Directory");
  
  // Build it as a Command object when you're ready to use it.
  Command command = builder.Build();
  
  // Execute the command through CommandRunner and get the results.
BufferedProcessResult result = await _commandRunner.ExecuteBufferedAsync(command);
Safe(r) Process Running

CliRunner also offers safer abstractions around Process Running to avoid accidentally not disposing of Processes after they are executed.

If directly executing the process and receiving a ProcessResult or BufferedProcessResult object is desirable you should use IProcessRunner as a service.

If you don't wish to immediately dispose of the Process after executing it, but plan to dispose of it later then IProcessRunnerUtility provides more flexibility.

IProcessRunner

Note: IProcessRunner and it's implementation class's execution methods do require a ProcessResultValidation argument to be passed, that configures whether validation should be performed on the Process' exit code. A default value for the parameter is intentionally not provided, as it is up to the user to decide whether they require exit code validation.

This example shows how it might be used:

using CliRunner.Runners;
using CliRunner.Runners.Abstractions;
// Using namespaces for Dependency Injection code ommitted for clarity

//Namespace and classs code ommitted for clarity 

      // ServiceProvider and Dependency Injection code ommitted for clarity

    IProcessRunner _processRunner = serviceProvider.GetRequiredService<IProcessRunner>();
    

    ProcessResult result = await _processRunner.ExecuteProcessAsync(process, ProcessResultValidation.None);

Asynchronous methods in IProcessRunner do provide an optional CancellationToken parameter.

Synchronous methods are available in IProcessRunner but should be used as a last resort, in situations where using async and await are not possible.

IProcessRunnerUtility

The naming of IProcessRunnerUtility is deliberately similar to IProcessRunner as it is the utility interface (and corresponding implementing class) that IProcessRunner uses behind the scenes for functionality.

Usage of IProcessRunnerUtility is most appropriate when greater flexibility is required than what IProcessRunner provides.

For instance, you can keep a Process object alive for as long as needed, and then dispose of it later.

using CliRunner.Runners;
using CliRunner.Runners.Abstractions;
// Using namespaces for Dependency Injection code ommitted for clarity

//Namespace and classs code ommitted for clarity 

      // ServiceProvider and Dependency Injection code ommitted for clarity

    IProcessRunnerUtility _processRunnerUtility = serviceProvider.GetRequiredService<IProcessRunnerUtility>();
    
    // Result Validation and Cancellation token are optional parameters.
    int exitCode = await _processRunnerUtility.ExecuteAsync(process);
    
    // Getting the result afterwards is done manually.
    ProcessResult = await _processRunnerUtility.GetResultAsync(process);
    
    // Code continuing to use process object ommitted.
    
    
    // Dispose of Process when no longer needed.
    _processRunnerUtility.DisposeOfProcess(process);

Some synchronous methods are available in IProcessRunnerUtility but should be used as a last resort, in situations where using async and await are not possible.

License

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

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

Assets

CliRunner's Icon is NOT licensed under the MPL 2.0 license and are licensed under Copyright with all rights reserved to me (Alastair Lundy).

If you fork CliRunner and re-distribute it, please replace the usage of the icon unless you have prior written agreements from me.

Acknowledgements

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

  • CliWrap for inspiring this project
  • Polyfill for simplifying .NET Standard 2.0 & 2.1 support
  • 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 net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on CliRunner:

Package Downloads
PlatformKit

Cross-platform Operating System Detection, Operating System Version Detection, Apple Silicon Mac Detection, programmatic .NET Runtime ID Generation, and more. For a full list of features please visit: https://github.com/alastairlundy/PlatformKit/blob/main/docs/FeatureComparisonByPlatform.md

CliRunner.Specializations

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

CliRunner.Extensions

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

CliRunner.Extensibility

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

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-beta.3 61 2/20/2025
1.0.0-beta.2 79 2/18/2025
1.0.0-beta.1 86 2/14/2025

## Changes since 1.0.0 Alpha 4

### Final Breaking Changes before 1.0
* Moved Dependency Injection extensions to a separate library.
* Renamed ``UseResourcePolicy`` extension method to ``SetResourcePolicy``
* Changed Process Running classes to make use of ProcessResourcePolicy

### Non-Breaking Changes
* Added ``HasStarted()`` Process extension method
* Added support for CliRunnert to resolve Target Files set to PATH variable file paths.
* Added ``IFilePathResolver`` and ``FilePathResolver``

### Bug Fixes
* Fixed a bug that would throw an exception due to setting ProcessResourcePolicy objects to Processes before they were started
* Fixed a bug that would throw an exception due to Standard Input attempting to be redirected when nulll.
* Fixed a bug where the default ProcessorAffinity for a ProcessResourcePolicy would be an invalid value.
* Fixed a bug where ProcessPipeHandler would cause an exception