NetLah.Extensions.CommandLineUtils Command line parsing for .NET.
<ItemGroup>
<PackageReference Include="NetLah.Extensions.CommandLineUtils" />
</ItemGroup>
CommandLineApplication commandLineApplication =
new(throwOnUnexpectedArg: true)
{
Name = "SampleCommandLine.exe",
FullName = "SampleCommandLine: greeting a fullname"
};
CommandArgument? names = null;
commandLineApplication.Command("hello",
(target) =>
{
target.FullName = "Greeting a fullname";
target.Description = "The hello command";
target.ShortVersionGetter = GetShortVersion;
target.LongVersionGetter = GetLongVersion;
names = target.Argument(
"fullname",
"Enter the full name of the person to be greeted.",
multipleValues: true);
target.HelpOption("-? | -h | --help");
target.OnExecute(() =>
{
if (names?.Values.Any() == true)
{
Greet(names.Values);
}
else
{
// show help if the required argument is missing
commandLineApplication.ShowHelp();
}
return 0;
});
});
commandLineApplication.HelpOption("-? | -h | --help");
commandLineApplication.VersionOption("--version", GetShortVersion, GetLongVersion);
commandLineApplication.OnExecute(() =>
{
// show help if root command
commandLineApplication.ShowHelp();
return 0;
});
commandLineApplication.Execute(args);
static void Greet(IEnumerable<string> values) => Console.WriteLine($"Hello {string.Join(" ", values)}!");
static string GetShortVersion() => "1.2.3";
static string GetLongVersion() => "v1.2.3+456abcd";
To know the available commands and options SampleCommandLine.exe --help
SampleCommandLine: greeting a fullname 1.2.3
Usage: SampleCommandLine.exe [options] [command]
Options:
-? | -h | --help Show help information
--version Show version information
Commands:
hello The hello command
Use "SampleCommandLine.exe [command] --help" for more information about a command.
To know the available arguments and options of a command SampleCommandLine.exe hello --help
Greeting a fullname 1.2.3
Usage: SampleCommandLine.exe hello [arguments] [options]
Arguments:
fullname Enter the full name of the person to be greeted.
Options:
-? | -h | --help Show help information
Command line SampleCommandLine.exe hello John Doe
Hello John Doe!
Command line SampleCommandLine.exe --version
SampleCommandLine: greeting a fullname
v1.2.3+456abcd
This repos a fork of Microsoft.Extensions.CommandLineUtils. Microsoft announces discontinue support the library. You may check this repos if prefer the more enrich features.