• Stars
    star
    638
  • Rank 68,034 (Top 2 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸƒ A .NET library that runs external commands.

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.

Platform support: .NET 6.0 and later.

Quick start

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

Run

Run("foo");
Run("foo", "arg1 arg2");
Run("foo", new[] { "arg1", "arg2" });

await RunAsync("foo");
await RunAsync("foo", "arg1 arg2");
await RunAsync("foo", new[] { "arg1", "arg2" });

By default, the command is echoed to standard output (stdout) for visibility.

Read

var (standardOutput1, standardError1) = await ReadAsync("foo");
var (standardOutput2, standardError2) = await ReadAsync("foo", "arg1 arg2");
var (standardOutput3, standardError3) = await ReadAsync("foo", new[] { "arg1", "arg2" });

Other optional arguments

string workingDirectory = "",
bool noEcho = false,
string? echoPrefix = null,
Action<IDictionary<string, string?>>? configureEnvironment = null,
bool createNoWindow = false,
Encoding? encoding = null,
Func<int, bool>? handleExitCode = null,
string? standardInput = 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

Most programs return a zero exit code when they succeed and a non-zero exit code fail. However, there are some programs which return a non-zero exit code when they succeed. For example, Robocopy returns an exit code less than 8 when it succeeds and 8 or greater when a failure occurs.

The throwing of exceptions for specific non-zero exit codes may 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, when running Robocopy, exception throwing should be suppressed for an exit code less than 8:

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

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

var exitCode = 0;
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;

Run by Gregor Cresnar from the Noun Project.

More Repositories

1

bullseye

🎯 A .NET library for running a target dependency graph.
C#
810
star
2

minver

🏷 Minimalistic versioning using Git tags.
C#
790
star
3

xbehave.net

βœ– An xUnit.net extension for describing each step in a test with natural language.
C#
382
star
4

liteguard

πŸ”’ The most simple, unambiguous, and lightweight .NET guard clause library.
C#
258
star
5

bau

The C# task runner
C#
151
star
6

scriptcs-nancy

Nancy Script Pack for scriptcs
C#
35
star
7

dude

The portable C# script runner!
C#
20
star
8

simple-targets-csx

βŠ™ A minimalist target runner for C# scripts.
C#
17
star
9

netstandard-deck

A slide deck for "What is .NET Standard?".
JavaScript
7
star
10

bau-msbuild

A Bau plugin for running MSBuild
C#
7
star
11

dotnet-scripting-dialect

Draft proposal for .NET scripting "dialects"
6
star
12

stylecop-msbuild

Adds official StyleCop MSBuild targets to your project for running StyleCop analysis with every build.
5
star
13

blunt

Smoke testing for multi-targetting NuGet packages.
C#
5
star
14

adamralph.github.com

My website
CSS
3
star
15

h5h

An HTML5 highlighter
CSS
3
star
16

code-the-future-now

Code samples for https://sessionize.com/s/adamralph/code_the_future_now/21105
C#
2
star
17

bau-nuget

A Bau plugin for running NuGet commands
C#
2
star
18

cola

The portable LINQPad runner!
C#
2
star
19

domaindrivendesign

A .NET framework for building applications using Domain Driven Design.
C#
2
star
20

racer83

A recreation of one of the masterpieces of my youth
Visual Basic
2
star
21

netstandard-magic

C#
1
star
22

.github

Standard files for my repos
1
star
23

xbehave.github.io

HTML
1
star
24

nancy-deck

Slides for my talk 'Nancy, the super duper happy OSS .NET project'
JavaScript
1
star