Install CommandLineParser via NuGet
masterTo use the Command Line Parser Library in your .NET project, install it using the NuGet package manager.
NuGet Install CommandLineParserrepository·master·Indexed 26 days ago
https://github.com/commandlineparser/commandlineA command line parser library for CLR and .NET Standard that provides an API for defining switches, options, and verb commands with customizable help screens. Supports C#, F#, and VB.NET, allowing developers to define options via attributes and handle parsed arguments using Parser.Default.ParseArguments.
To use the Command Line Parser Library in your .NET project, install it using the NuGet package manager.
NuGet Install CommandLineParserIn version 1.9.4.207, several core types were renamed to simplify the API. If you are upgrading from an older version, you must update your code to use the following names:
CommandLineParser $\rightarrow$ ParserICommandLineParser $\rightarrow$ IParserCommandLineParserSettings $\rightarrow$ ParserSettingsCommandLineParserException $\rightarrow$ ParserExceptionThe error handling mechanism has undergone two breaking changes:
HandleParsingErrorsDelegate was renamed to ParsingErrorsHandler.ParsingErrorsHandler delegate was replaced by Action<HelpText>.Ensure your error handling logic uses the Action<HelpText> signature for modern versions.
OptionAttribute is now sealed. Additionally, OptionArrayAttribute and OptionListAttribute now derive from BaseOptionAttribute. If you have created custom attribute types that inherit from these, you must update your custom types to derive from BaseOptionAttribute instead.GetVerbOptionsInstanceByName was marked as obsolete. You should instead use HelpText.AutoBuild(object, string) to achieve the same functionality.In VB.NET, decorate option classes with <CommandLine.Verb(...)>. Use CommandLine.Parser.Default.ParseArguments(Of T1, T2, ...)(args) and .MapResult to handle the different verb types.
<CommandLine.Verb("add", HelpText:="Add file contents to the index.")>
Public Class AddOptions
'Normal options here
End Class
<CommandLine.Verb("commit", HelpText:="Record changes to the repository.")>
Public Class CommitOptions
'Normal options here
End Class
<CommandLine.Verb("clone", HelpText:="Clone a repository into a new directory.")>
Public Class CloneOptions
'Normal options here
End Class
Function Main(ByVal args As String()) As Integer
Return CommandLine.Parser.Default.ParseArguments(Of AddOptions, CommitOptions, CloneOptions)(args) _
.MapResult(
(Function(opts As AddOptions) RunAddAndReturnExitCode(opts)),
(Function(opts As CommitOptions) RunCommitAndReturnExitCode(opts)),
(Function(opts As CloneOptions) RunCloneAndReturnExitCode(opts)),
(Function(errs As IEnumerable(Of [Error])) 1)
)
End FunctionTo implement basic command line parsing in C#, define a class to hold your options decorated with the [Option] attribute, then use Parser.Default.ParseArguments<T> to parse the arguments and handle the result using .WithParsed<T>.
using System;
using CommandLine;
namespace QuickStart
{
class Program
{
public class Options
{
[Option('v', "verbose", Required = false, HelpText = "Set output to verbose messages.")]
public bool Verbose { get; set; }
}
static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
if (o.Verbose)
{
Console.WriteLine($"Verbose output enabled. Current Arguments: -v {o.Verbose}");
Console.WriteLine("Quick Start Example! App is in Verbose mode!");
}
else
{
Console.WriteLine($"Current Arguments: -v {o.Verbose}");
Console.WriteLine("Quick Start Example!");
}
});
}
}
}git commit, create separate option classes for each verb and decorate them with the [Verb] attribute. Use Parser.Default.ParseArguments<T1, T2, ...> and MapResult to route the execution flow to the appropriate handler based on which verb was provided.CommandLineParser.FSharp package. Define your verb options as types decorated with the [<Verb>] attribute. Use Parser.Default.ParseArguments<T1, T2, ...> and pattern match on the result to handle the parsed command.