Giter Club home page Giter Club logo

simple-exec's Introduction

SimpleExec

SimpleExec

NuGet version

Build status CodeQL analysis Lint Spell check

SimpleExec is a .NET library that runs external commands. It wraps System.Diagnostics.Process to make things easier.

SimpleExec intentionally does not invoke the system shell.

By default, the command is echoed to standard error (stderr) for visibility.

Platform support: .NET Standard 2.1 and later.

Quick start

using static SimpleExec.Command;
Run("foo.exe", "arg1 arg2");

API

Run

Run("foo.exe");
Run("foo.exe", "arg1 arg2", "my-working-directory");

await RunAsync("foo.exe");
await RunAsync("foo.exe", "arg1 arg2", "my-working-directory");

Read

var (standardOutput1, standardError1) = await ReadAsync("foo.exe");
var (standardOutput2, standardError2) = await ReadAsync("foo.exe", "arg1 arg2", "my-working-directory");

Other optional arguments

bool noEcho = false,
string? windowsName = null,
string? windowsArgs = null,
string? echoPrefix = null,
Action<IDictionary<string, string>>? configureEnvironment = null,
bool createNoWindow = false,
Func<int, bool>? handleExitCode = null,
CancellationToken cancellationToken = default,

Exceptions

If the command has a non-zero exit code, an ExitCodeException is thrown with an int ExitCode property and a message in the form of:

$"The process exited with code {ExitCode}."

In the case of ReadAsync, an ExitCodeReadException is thrown, which inherits from ExitCodeException, and has string Out and Error properties, representing standard out (stdout) and standard error (stderr), and a message in the form of:

$@"The process exited with code {ExitCode}.

Standard Output:

{Out}

Standard Error:

{Error}"

Overriding default exit code handling

The throwing of exceptions for specific non-zero exit codes can be suppressed by passing a delegate to handleExitCode which returns true when it has handled the exit code and default exit code handling should be suppressed, and returns false otherwise. For example:

Run("ROBOCOPY", "from to", handleExitCode: code => code < 8);

Note that it may be useful to record the exit code. For example:

var exitCode = 0;
var result = Run("ROBOCOPY", "from to", handleExitCode: code => (exitCode = code) < 8);

// see https://ss64.com/nt/robocopy-exit.html
var oneOrMoreFilesCopied = exitCode & 1;
var extraFilesOrDirectoriesDetected = exitCode & 2;
var misMatchedFilesOrDirectoriesDetected = exitCode & 4;

Windows

๐Ÿ™„

Sometimes, for whatever wonderful reasons, it's necessary to run a different command on Windows. For example, when running Yarn, some combination of mysterious factors may require that you explicitly run cmd.exe with Yarn as an argument, rather than running Yarn directly. The optional windowsNames and windowsArgs parameters may be used to specify a different command name and arguments for Windows:

Run("yarn", windowsName: "cmd", windowsArgs: "/c yarn");

Run by Gregor Cresnar from the Noun Project.

simple-exec's People

Contributors

adamralph avatar dependabot[bot] avatar khalidabuhakmeh avatar pauldotknopf avatar pgermishuys avatar rasmus-unity 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.