CommandLineParser Documentation

repository·master·Indexed 26 days ago

https://github.com/commandlineparser/commandline

A 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.

Tokens
1.6K
Snippets
3
Records
10
Agent score
39%

What's inside CommandLineParser

  1. Migrate to Parser and ParserSettings (v1.9.4.207+)

    master

    In 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$ Parser
    • ICommandLineParser $\rightarrow$ IParser
    • CommandLineParserSettings $\rightarrow$ ParserSettings
    • CommandLineParserException $\rightarrow$ ParserException
  2. Update ParsingErrorsHandler usage (v1.9.4.123 and v1.9.4.211+)

    master

    The error handling mechanism has undergone two breaking changes:

    1. v1.9.4.123: HandleParsingErrorsDelegate was renamed to ParsingErrorsHandler.
    2. v1.9.4.211: The ParsingErrorsHandler delegate was replaced by Action<HelpText>.

    Ensure your error handling logic uses the Action<HelpText> signature for modern versions.

  3. Update OptionAttribute and custom attributes (v1.9.4.127+)

    master
    Starting in version 1.9.4.127, 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.
  4. Implement Verb Commands in VB.NET

    master

    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 Function
  5. Quick Start with C#

    master

    To 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!");
                           }
                       });
            }
        }
    }
  6. Implement Verb Commands in C#

    master
    To support multiple commands (verbs) like 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.