ConsoleRunnerLib 1.0.2

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

// Install ConsoleRunnerLib as a Cake Tool
#tool nuget:?package=ConsoleRunnerLib&version=1.0.2                

CustomRunnerLib

The modern and easiest Console Runner adapted to DI principles.

Combine the power of Console apps with cancellation capability and custom logging.

How to install

Via tha Package Manager:

Install-Package CustomRunnerLib

Via the .NET CLI

dotnet add package CustomRunnerLib

How to use via DI

The common way to use the library is to add the library to the ServiceCollection to the host builder:

IHost Host.CreateDefaultBuilder().ConfigureServices((context,services) =>
{
    services
        //we assume that the Driver will use the library (example given later)
        .AddSingleton<Driver>() 
        //this adds the implementations for IConsoleRunner, IConsoleLogger
        .AddConsoleRunnerLib() 
}).Build();

IConsoleRunner

The IConsoleRunner contains exactly 3 methods to run a console application:

 public interface IConsoleRunner
 {
    //The events are used only when the RunWithEvents is called
     event EventHandler<DataReceivedEventArgs>? ErrorReceived;
     event EventHandler<DataReceivedEventArgs>? OutputReceived;

     Task<string?> Run(string executablePath, string arguments, string workingDirectory = "");
     Task<ProcessOutput> RunAndGetOutputAndError(string executablePath, string arguments, string workingDirectory = "");
     Task<int> RunWithEvents(string executablePath, string arguments, string workingDirectory = "", CancellationToken cancellationToken = default);
 }

In general an console application will output messages in both the standard output and standard error streams. If the output is expected to be minimal and we do not expect messages at the standard error stream, we can call the simple Run method. In the example below, we want to retrieve the title of a video file with the yt-dlp executable:

public class Driver
{
    private readonly IConsoleRunner _process;

    private readonly string _exePath = "c:/tools/yt-dlp.exe";
    private readonly ILogger<Driver> _logger;
    private readonly IServiceProvider _provider;

    public Driver(IConsoleRunner process, ILogger<Driver> logger, IServiceProvider provider)
    {
        _process = process;
        _logger = logger;
        _provider = provider;
    }


public async Task<string?> GetTitle(string videoId) =>
    await _process.Run(_exePath, $" --get-title -- {videoId}");
}

If we expect output for the standard error stream, then we call the more generic RunAndGetOutputAndError method. The ProcessOutput struct is returned which is the following:

public readonly struct ProcessOutput
{
    public string StandardOutput { get; init; }
    public string StandardError { get; init; }
    public int ExitCode { get; init; }
}

Therefore we can inspect both the output and error streams, as well as the exit code of the console app, and act accordingly. For example:

    ProcessOutput output =  await _process.RunAndGetOutputAndError(_exePath, $" --get-title -- {videoId}");
    if(output.StandardError.Length > 0 )
        _logger.LogError(output.StandardError);
    else
        _logger.LogInformation(output.StandardOutput);

Long runs

For long runs, the RunWithEvents give full control capability of the application.

private async Task RunProcessExample()
{
    IConsoleRunner process =  _provider.GetConsoleRunner();
    process.OutputReceived += (object? s, DataReceivedEventArgs args) =>
    {
        string? status = args.Data;
        if (string.IsNullOrWhiteSpace(status)) return;
        
        //do something
        _logger.LogInformation(status);
    };

    process.ErrorReceived += (object? s, DataReceivedEventArgs args) =>
    {
        string? status = args.Data;
        if (string.IsNullOrWhiteSpace(status)) return;

        //do something
        _logger.LogError(status);
    };
    //...
    //we assume argeuments and workingDirectory are defined before
    int exitCode = await process.RunWithEvents(_exePath, arguments, workingDirectory);
}

STAY TUNED

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.1.0 81 12/8/2024
1.0.6 151 3/31/2024
1.0.5 115 3/31/2024
1.0.4 111 3/31/2024
1.0.2 118 3/31/2024