Feature rich library to parse command line arguments in C# Applications
dotnet nuget push "PATH TO NUPKG" --api-key YOUR_PAT --source "github"
NuGet.exe pack CommandLineArgumentsParser.csproj -Prop Configuration=Release
C:\Users\USERNAME\AppData\Roaming\NuGet\NuGet.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="github" value="https://nuget.pkg.github.com/CleverCodeCravers/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="CleverCodeCravers" />
<add key="ClearTextPassword" value="<PERSONAL ACCESS TOKEN FOR YOUR USER>" />
</github>
</packageSourceCredentials>
</configuration>
This is an advance approach for using the library, a simpler way is also possible.
public record ConsoleCommandLineParameters(bbol Configure, string TargetDirectory, string ScriptsDirectory, int Count);
using CommandLineArgumentsParser;
public class ConsoleCommandLineParser
{
public Result<ConsoleCommandLineParameters> Parse(string[] args)
{
var parser = new Parser(
new ICommandLineOption[] {
new BoolCommandLineOption("--configure", "configures the app"),
new StringCommandLineOption("--targetDirectory", "The directory where the files should be saved e.g"),
new StringCommandLineOption("--scriptsDirectory", "The directory where the scripts live"),
new Int32CommandLineOption("--count", "How many times the app should run!")
});
if (!parser.TryParse(args, true) || args.Length == 0)
{
return Result<ConsoleCommandLineParameters>.Failure("Invalid command line arguments");
}
return Result<ConsoleCommandLineParameters>.Success(
new ConsoleCommandLineParameters(
parser.GetBoolOption("--configure"),
parser.GetOptionWithValue<string>("--targetDirectory") ?? "",
parser.GetOptionWithValue<string>("--scriptsDirectory") ?? "",
parser.GetOptionWithValue<int>("--count") ?? 0,
));
}
}
var parser = new ConsoleCommandLineParser();
var commandLineParametersResult = parser.Parse(args);
if (!commandLineParametersResult.IsSuccess)
{
Console.WriteLine("Unfortunately there have been problems with the command line arguments.");
return;
}
var parameters = commandLineParametersResult.Value;
var parser = new ConsoleCommandLineParser();
var commandLineParametersResult = parser.Parse(args);
if (!commandLineParametersResult.IsSuccess)
{
Console.WriteLine("Unfortunately there have been problems with the command line arguments.");
return;
}
// The GetCommandsList() will return an array of ICommandLineOption[] which can be helpful when creating help command
ICommandLineOption[] commands = parser.GetCommandsList();