MichelMichels.CliSharp 2.0.0

dotnet add package MichelMichels.CliSharp --version 2.0.0                
NuGet\Install-Package MichelMichels.CliSharp -Version 2.0.0                
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="MichelMichels.CliSharp" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MichelMichels.CliSharp --version 2.0.0                
#r "nuget: MichelMichels.CliSharp, 2.0.0"                
#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 MichelMichels.CliSharp as a Cake Addin
#addin nuget:?package=MichelMichels.CliSharp&version=2.0.0

// Install MichelMichels.CliSharp as a Cake Tool
#tool nuget:?package=MichelMichels.CliSharp&version=2.0.0                

CliSharp 💻

NuGet version

This project contains a C# library to easily execute external command line tools on Windows OS.

Table of contents


Getting Started

Clone the repository or get the NuGet package from NuGet.org.

Example

The API is implemented with a fluent design and starts with a static method call on the Cli class.

using MichelMichels.CliSharp.Core;
using MichelMichels.CliSharp.Extensions;

(...)

await Cli
    .SetProgram("notepad.exe")
    .SetTimeout(TimeSpan.FromSeconds(1))
    .AddSwitch(...)
    (...)
    .AddConditionalSwitch(...)
    (...)
    .Execute()
    .Wait();

(...)

.SetProgram(...)

The SetProgram method sets the executable's path.

.SetTimeout(...)

The SetTimeout method adds a timeout for when to kill the process and return to the original thread.

AddSwitch(...)

The .AddSwitch(...) method accepts CommandLineSwitch-objects or string-objects as arguments to create a switch.

Possible switch types are:

  • CommandLineSwitch - e.g. "/help"
  • CommandLineSwitch<T> - e.g. "/output test.txt"
  • CommandLineSwitchPrefix<T> - e.g. "/drive label=Test", in which "label" is the prefix

AddConditionalSwitch(...)

The .AddConditionalSwitch method does the same as the AddSwitch method, but it's possible to add a boolean condition as argument. This can come in handy when using variable parameters.

.Execute()

The Execute method executes the command and will throw an ExitCodeException if the returned ExitCode isn`t 0.

.Wait()

The Wait method enables await and waits for the timeout or program to complete.

Using enums

Using enums for CommandLineSwitch values is supported. The library will use the enum name as a string or you can use the attribute ParameterName from CliSharp.Attributes in your enum to override the value.

enum TestValue
{
    [ParameterName("r")]
    Red,

    [ParameterName("g")]
    Green,

    [ParameterName("b")]
    Blue
}

Running the tests

There is a MsTest library included MichelMichels.CliSharpTests which can be run with the Test Explorer in Visual Studio.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License.

Acknowledgments

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.
  • net8.0

    • No dependencies.

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
2.0.0 121 5/26/2024

- Release 2.0.0
     - Breaking namespace change to MichelMichels.CliSharp
     - Update to .NET 8