CLArgs - The .NET command-line parser

Documentation

CLArgs (Command-Line Arguments) is a Command-Line parser for .NET!

  • The most powerful

  • The easiest to use

  • The most flexible

CLArgs V2 is completely new! It was built with the latest .NET 6 builder concepts in mind.

Version 1 is still part of the CLArgs Assembly so that your existing v1 application won't break. However, v1 is not longer supported or maintained and I want to encourage you to upgrade to version 2!

class Program
{
    public static void Main(string[] args)
    {
        CommandHostBuilder.Create(args).Start();
    }
}

The implementation of the application's functionality, together with all the help-information is provided using Commands and Contexts.

The Command and the Context

A Command implements the functionality that is executed (Verb). A Command uses a Context to define the supported arguments.

[Command( "HelloWorld", 
  helpText: "A simple command to print a name in the Console Window.")]
public class HelloWorldCommand : CommandBase2<HelloWorldContext>
{
    public HelloWorldCommand(IServiceProvider serviceProvider) 
        : base(serviceProvider) {}

    protected override void Execute()
    {
        // Use the Context of type HelloWorldContext
        // to access your command-line arguments.
        Console.WriteLine($"Hello {Context.Name}");
    }
}

The HelloWorldContext describes a single option, only.

public class HelloWorldContext
{
    [OptionDescriptor("Name", new[] { "n" },
        Default = "John Doe", Required = false,
        HelpText = "Specify the name of the person you want to say 'Hello'.")]
    public string? Name { get; set; }
}

See how to display help.

Use your application

> yourApp /n "Markus Schmidt"
Hello Markus Schmidt
> yourApp /?

HelloWorld          A simple command to print a name in the Console Window.
--------------------------------------------------------------------------------
/Name               Tags=n optional
                    Specify the name of the person you want to say 'Hello'.
                    DEFAULT: 'John Doe'

What's next

Last updated