This project contains a C# library to easily execute external command line tools on Windows OS.
Clone the repository or get the NuGet package from NuGet.org.
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();
(...)
The SetProgram
method sets the executable's path.
The SetTimeout
method adds a timeout for when to kill the process and return to the original thread.
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
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.
The Execute
method executes the command and will throw an ExitCodeException
if the returned ExitCode
isn`t 0.
The Wait
method enables await
and waits for the timeout or program to complete.
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
}
There is a MsTest library included MichelMichels.CliSharpTests
which can be run with the Test Explorer in Visual Studio.
- Michel Michels - Initial work - MichelMichels
See also the list of contributors who participated in this project.
This project is licensed under the MIT License.
- Inspired by CommandLineParser