Skip to content

Latest commit

 

History

History
116 lines (77 loc) · 3.14 KB

README.md

File metadata and controls

116 lines (77 loc) · 3.14 KB

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