Introduction
Switch is a very simple to configure command line parser.
Unlike others, it is not opinionated about how you execute your command, or organise your command structure.
It just parses a command list, and it is intended that is all it will ever do.
Getting Started!
- Install the switch package onto your project:
dotnet add package webefinity-switch
- Construct an argument builder.
var builder = new ArgumentBuilder();
- Add parameters.
builder.Add("param", 'p').AcceptString().MakeRequired().WithDescription("A parameter to love.");
builder.Add("help", '?').AcceptFlag().WithDescription("Get some help.");
- Build the builder to get back a handler which contains your validated parameters.
var handler = builder.Build();
- Get your parameters value.
var paramValue = handler.GetValue<string>("param");
- Add in some usage warnings.
Before getting any parameter values, include the following to show using using Console.Write:
if (optionsHandler.GetValue<bool>("help") || !optionsHandler.IsValid)
{
optionsHandler.Usage("My Console App", "Version 1.0");
return;
}
The Full Example
// Define our arguments
var builder = new ArgumentBuilder();
builder.Add("param", 'p').AcceptString().MakeRequired().WithDescription("A parameter to love.");
builder.Add("help", '?').AcceptFlag().WithDescription("Get some help.");
// Parse arguments from Environment.GetCommandLineArgs()
var handler = builder.Build();
// If args were not valid, or help was flagged
if (optionsHandler.GetValue<bool>("help") || !optionsHandler.IsValid)
{
optionsHandler.Usage("My Console App", "Version 1.0");
return;
}
// Otherwise use the arguments
var paramValue = handler.GetValue<string>("param");
Console.Writeline(paramValue);