Giter Club home page Giter Club logo

cliwrap's Introduction

CliWrap

Build Tests NuGet NuGet

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
  • Abort all executions at once, on demand
  • Set up callbacks that trigger when a process writes to StdOut or StdErr
  • Targets .NET Framework 4.5+, .NET Core 1.0+ and .NET Standard 2.0+
  • No external dependencies

Usage

The Cli class was designed to have each instance treated as a member of your project. When you're wrapping around a command line interface, you can think of the interface itself as a singleton class and all its commands as methods of that class. Therefore, it is recommended to create a reusable instance of Cli for each target executable.

If you're executing processes that can potentially outlive the parent, especially if they use a lot of resources, it is recommended to call CancelAll() at some point before your application terminates.

Execute a command and handle output
using (var cli = new Cli("some_cli.exe"))
{
    // Execute
    var output = await cli.ExecuteAsync("command --option");
    // ... or in synchronous manner:
    // var output = cli.Execute("command --option");

    // Throw an exception if CLI reported an error
    output.ThrowIfError();

    // Extract output
    var code = output.ExitCode;
    var stdOut = output.StandardOutput;
    var stdErr = output.StandardError;
    var startTime = output.StartTime;
    var exitTime = output.ExitTime;
    var runTime = output.RunTime;
}
Execute a command without waiting for completion
using (var cli = new Cli("some_cli.exe"))
{
    cli.ExecuteAndForget("command --option");
}
Pass in standard input
using (var cli = new Cli("some_cli.exe"))
{
    var input = new ExecutionInput("command --option", "this is stdin");
    var output = await cli.ExecuteAsync(input);
}
Pass in environment variables
using (var cli = new Cli("some_cli.exe"))
{
    var input = new ExecutionInput("command --option");
    input.EnvironmentVariables.Add("some_var", "some_value");
    var output = await cli.ExecuteAsync(input);
}
Cancel execution
using (var cli = new Cli("some_cli.exe"))
using (var cts = new CancellationTokenSource())
{
    cts.CancelAfter(TimeSpan.FromSeconds(1)); // e.g. timeout of 1 second
    var output = await cli.ExecuteAsync("command --option", cts.Token);
}
Handle real-time standard output/error data
using (var cli = new Cli("some_cli.exe"))
{
    var handler = new BufferHandler(
            stdOutLine => Console.WriteLine("StdOut> " + stdOutLine),
            stdErrLine => Console.WriteLine("StdErr> " + stdErrLine));

    var output = await cli.ExecuteAsync("command --option", bufferHandler: handler);
}

cliwrap's People

Contributors

brunozell avatar jakoss avatar pipe01 avatar tyrrrz avatar

Watchers

 avatar  avatar

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.