Git Product home page Git Product logo

cliwrap's Introduction

CliWrap

Build Tests NuGet NuGet Donate Donate

CliWrap is a library that makes it easier to interact with command line interfaces. It provides a convenient wrapper around the target executable, allowing you to pass execution parameters and read the resulting output. The library can also handle errors reported by the underlying process, allows command cancellation and has both synchronous and asynchronous APIs.

Download

Features

  • Full abstraction over System.Diagnostics.Process
  • Execute commands in a synchronous, asynchronous, fire-and-forget manner
  • Pass in command line arguments, standard input and environment variables
  • Get process exit code, standard output and standard error as the result
  • Abort the execution early using System.Threading.CancellationToken
  • Set up callbacks that trigger when a process writes to standard output or error
  • Custom encoding settings for standard input, output and error
  • Fluent interface
  • Targets .NET Framework 4.5+ and .NET Standard 2.0+
  • No external dependencies

Usage

Execute a command

var result = await Cli.Wrap("cli.exe")
    .SetArguments("Hello world!")
    .ExecuteAsync();

var exitCode = result.ExitCode;
var stdOut = result.StandardOutput;
var stdErr = result.StandardError;
var startTime = result.StartTime;
var exitTime = result.ExitTime;
var runTime = result.RunTime;

Standard input

var result = await Cli.Wrap("cli.exe")
    .SetStandardInput("Hello world from stdin!") // can also pipe a stream instead
    .ExecuteAsync();

Environment variables

var result = await Cli.Wrap("cli.exe")
    .SetEnvironmentVariable("var1", "value1")
    .SetEnvironmentVariable("var2", "value2")
    .ExecuteAsync();

Cancel execution

using (var cts = new CancellationTokenSource())
{
    cts.CancelAfter(TimeSpan.FromSeconds(5)); // e.g. timeout of 5 seconds

    var result = await Cli.Wrap("cli.exe")
        .SetCancellationToken(cts.Token)
        .ExecuteAsync();
}

Callbacks for stdout and stderr

var result = await Cli.Wrap("cli.exe")
    .SetStandardOutputCallback(l => Console.WriteLine($"StdOut> {l}")) // triggered on every line in stdout
    .SetStandardErrorCallback(l => Console.WriteLine($"StdErr> {l}")) // triggered on every line in stderr
    .ExecuteAsync();

Error handling

var result = await Cli.Wrap("cli.exe")
    .EnableExitCodeValidation(true) // throw exception on non-zero exit code (on by default)
    .EnableStandardErrorValidation(true) // throw exception on non-empty stderr (off by default)
    .ExecuteAsync();

Libraries used

Donate

If you really like my projects and want to support me, consider donating to me on Patreon or BuyMeACoffee. All donations are optional and are greatly appreciated. ๐Ÿ™

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.