An open API service indexing awesome lists of open source software.

https://github.com/alastairlundy/CliInvoke

CliRunner is a .NET Library for interacting with Command Line Interfaces, CliRunner is a library for interacting with Command Line Interfaces and wrapping around executables.
https://github.com/alastairlundy/CliInvoke

cli csharp-library dotnet process shell

Last synced: 8 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# CliInvoke

[![Latest NuGet](https://img.shields.io/nuget/v/AlastairLundy.CliInvoke.svg)](https://www.nuget.org/packages/AlastairLundy.CliInvoke/)
[![Latest Pre-release NuGet](https://img.shields.io/nuget/vpre/AlastairLundy.CliInvoke.svg)](https://www.nuget.org/packages/AlastairLundy.CliInvoke/)
[![Downloads](https://img.shields.io/nuget/dt/AlastairLundy.CliInvoke.svg)](https://www.nuget.org/packages/AlastairLundy.CliInvoke/)
![License](https://img.shields.io/github/license/alastairlundy/CliInvoke)

CliInvoke Logo

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 much more.

## Table of Contents
* [Features](#features)
* [Why CliInvoke?](#why-cliinvoke)
* [Installing CliInvoke](#installing-cliinvoke)
* [Installing CliInvoke](#installing-CliInvoke)
* [Supported Platforms](#supported-platforms)
* [CliInvoke Examples](#examples)
* [Contributing to CliInvoke](#how-to-contribute-to-cliinvoke)
* [Roadmap](#cliinvokes-roadmap)
* [License](#license)
* [Acknowledgements](#acknowledgements)

## Features
* Clear separation of concerns between Process Configuration Builders, Process Configuration Models, and Invokers.
* Supports .NET Standard 2.0, .NET 8 and newer TFMs, and has few dependencies.
* Has Dependency Injection extensions to make using it a breeze.
* Support for specific specializations such as running executables or commands via Windows Powershell or CMD on Windows 1
* [SourceLink](https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/sourcelink) support

1 Specializations library distributed separately.

## Why CliInvoke?

| Feature | CliInvoke | CliWrap | ProcessX |
|------------------------------------------------------------------------|:---------:|:-------:|:---------------------------------------:|
| Configure and Run External Processes using code written in .NET | ✅ | ✅ | ❌, Uses mixture of .NET and BASH syntax |
| No Additional Licensing Terms Required for Use | ✅ | ❌ | ✅ |
| Uses only managed .NET code | ✅ | ❌ | ✅ |
| Follows Separation of Concerns and the Single Responsibility Principle | ✅ | ❌ | ❌ |
| Allows for alternative Process Runners to be specified and/or used | ✅ | ❌ | ❌ |

## Installing CliInvoke
CliInvoke is available on [the Nuget Gallery](https://nuget.org) but call be also installed via the ``dotnet`` sdk cli.

The package(s) to install depends on your use case:
* For use in a .NET library - Install the [Abstractions Package](#abstractions-package), your developer users can install the Implementation and Dependency Injection packages.
* For use in a .NET app - Install the [Implementation Package](#implementation-package) and the [Dependency Injection Extensions Package](#dependency-injection-extensions)

### Abstractions Package
[CliInvoke.Core Nuget](https://nuget.org/packages/AlastairLundy.CliInvoke.Core)

```bash
dotnet add package AlastairLundy.CliInvoke.Core
```

### Implementation Package

[CliInvoke Nuget](https://nuget.org/packages/AlastairLundy.CliInvoke)

```bash
dotnet add package AlastairLundy.CliInvoke
```

### Extensions Package

[CliInvoke.Extensions Nuget](https://nuget.org/packages/AlastairLundy.CliInvoke.Extensions)

```bash
dotnet add package AlastairLundy.CliInvoke.Extensions
```

### Specializations Package
[CliInvoke.Specializations Nuget](https://nuget.org/packages/AlastairLundy.CliInvoke.Specializations)

```bash
dotnet add package AlastairLundy.CliInvoke.Specializations
```

## Supported Platforms
CliInvoke can currently be added to .NET Standard 2.0, .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 :white_check_mark: | |
| macOS | Fully Supported :white_check_mark: | |
| Mac Catalyst | Untested Platform :warning: | Support for this platform has not been tested but should theoretically work. |
| Linux | Fully Supported :white_check_mark: | |
| FreeBSD | Fully Supported :white_check_mark: | |
| Android | Untested Platform :warning: | Support for this platform has not been tested but should theoretically work. |
| IOS | Not Supported :x: | Not supported due to ``Process.Start()`` not supporting IOS. 3 |
| tvOS | Not Supported :x: | Not supported due to ``Process.Start()`` not supporting tvOS 3 |
| watchOS | Not Supported :x: | Not supported due to ``Process.Start()`` not supporting watchOS 4 |
| Browser | Not Planned :x: | Not supported due to not being a valid target Platform for executing programs or processes. |

3 - See the [Process class documentation](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.start?view=net-9.0#system-diagnostics-process-start) for more info.

4 - Lack of watchOS support is implied by lack of IOS support since [watchOS is based on IOS](https://en.wikipedia.org/wiki/WatchOS).

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

## Examples

### Simple ``ProcessConfiguration`` creation with Factory Pattern
This approach uses the ``IProcessConfigurationFactory`` interface factory to create a ``ProcessConfiguration``. It requires fewer parameters and sets up more defaults for you.

It can be provided with a ``Action configure`` optional parameter where greater control is desired.

#### Non-Buffered Execution Example
This example gets a non buffered ``ProcessResult`` that contains basic process exit code, id, and other information.

```csharp
using AlastairLundy.CliInvoke.Core.Factories;
using AlastairLundy.CliInvoke.Core;
using AlastairLundy.CliIinvoke;

using Microsoft.Extensions.DependencyInjection;

// Dependency Injection setup code ommitted for clarity

// Get IProcessConfigurationFactory
IProcessConfigurationFactory processConfigFactory = serviceProvider.GetRequiredService();

// Get IProcessConfigurationInvoker
IProcessConfigurationInvoker _invoker_ = serviceProvider.GetRequiredService();

// Simply create the process configuration.
ProcessConfiguration configuration = processConfigFactory.Create("path/to/exe", "arguments");

// Run the process configuration and get the results.
ProcessResult result = await _invoker.ExecuteAsync(configuration, CancellationToken.None);
```

#### Buffered Execution Example
This example gets a ``BufferedProcessResult`` which contains redirected StandardOutput and StandardError as strings.

```csharp
using AlastairLundy.CliInvoke.Core.Factories;
using AlastairLundy.CliInvoke.Core;
using AlastairLundy.CliIinvoke;

using Microsoft.Extensions.DependencyInjection;

// Dependency Injection setup code ommitted for clarity

// Get IProcessConfigurationFactory
IProcessConfigurationFactory processConfigFactory = serviceProvider.GetRequiredService();

// Get IProcessConfigurationInvoker
IProcessConfigurationInvoker _invoker_ = serviceProvider.GetRequiredService();

// Simply create the process configuration.
ProcessConfiguration configuration = processConfigFactory.Create("path/to/exe", "arguments");

// Run the process configuration and get the results.
BufferedProcessResult result = await _invoker.ExecuteBufferedAsync(configuration, CancellationToken.None);
```

### Advanced Configuration with Builders

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.

```csharp
using AlastairLundy.CliInvoke;
using AlastairLundy.CliInvoke.Core;

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

using Microsoft.Extensions.DependencyInjection;

//Namespace and class code ommitted for clarity

// ServiceProvider and Dependency Injection setup code ommitted for clarity

IProcessInvoker _processInvoker = serviceProvider.GetRequiredService();

// 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.

```csharp
using AlastairLundy.CliInvoke;
using AlastairLundy.CliInvoke.Builders;

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

using Microsoft.Extensions.DependencyInjection;

//Namespace and class code ommitted for clarity

// ServiceProvider and Dependency Injection setup code ommitted for clarity

IProcessInvoker _processInvoker = serviceProvider.GetRequiredService();

// Fluently configure your Command.
IProcessConfigurationBuilder builder = new ProcessConfigurationBuilder("Path/To/Executable")
.WithArguments(["arg1", "arg2"])
.WithWorkingDirectory("/Path/To/Directory")
.RedirectStandardOutput(true)
.RedirectStandardError(true);

// 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 _processInvoker.ExecuteBufferedAsync(config);
```

## How to Build CliInvoke's code
Please see [building-cliinvoke.md](docs/docs/building-cliinvoke.md) for how to build CliInvoke from source.

## How to Contribute to CliInvoke
Please see the [CONTRIBUTING.md file](CONTRIBUTING.md) 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](https://github.com/alastairlundy/CliInvoke/issues/) to see if a similar or identical issue is already open.
If there isn't already a relevant issue filed, please [file one here](https://github.com/alastairlundy/CliInvoke/issues/new) and follow the respective guidance from the appropriate issue template.

## 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.

Future updates may 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. You can learn more about it [here](https://www.mozilla.org/en-US/MPL/)

If you use CliInvoke in your project please make an exact copy of the contents of CliInvoke's [LICENSE.txt file](https://github.com/alastairlundy/CliInvoke/blob/main/LICENSE.txt) 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:
* [CliWrap](https://github.com/Tyrrrz/CliWrap/) for inspiring this project
* [Polyfill](https://github.com/SimonCropp/Polyfill) for simplifying .NET Standard 2.0 support

For more information, please see the [THIRD_PARTY_NOTICES file](https://github.com/alastairlundy/CliInvoke/blob/main/THIRD_PARTY_NOTICES.txt).