McMaster.Extensions.CommandLineUtils 2.1.0-alpha

Command-line parsing API. A community-maintained fork of Microsoft.Extensions.CommandLineUtils, plus extras.
Commonly used types:

McMaster.Extensions.CommandLineUtils.CommandLineApplication
McMaster.Extensions.CommandLineUtils.CommandOption
McMaster.Extensions.CommandLineUtils.IConsole
McMaster.Extensions.CommandLineUtils.ArgumentEscaper

This is a prerelease version of McMaster.Extensions.CommandLineUtils.
There is a newer prerelease version of this package available.
See the version list below for details.
Install-Package McMaster.Extensions.CommandLineUtils -Version 2.1.0-alpha
dotnet add package McMaster.Extensions.CommandLineUtils --version 2.1.0-alpha
paket add McMaster.Extensions.CommandLineUtils --version 2.1.0-alpha
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Usage

See detailed examples, see the samples for more examples, such as:

CommandLineApplication is the main entry point for most console apps parsing. There are two primary ways to use this API, using the builder pattern and attributes.

Attribute API

using System;
using McMaster.Extensions.CommandLineUtils;

[HelpOption]
public class Program
{
    public static int Main(string[] args)
        => CommandLineApplication.Execute<Program>(args);

    [Option(Description = "The subject")]
    public string Subject { get; }

    private void OnExecute()
    {
        var subject = Subject ?? "world";
        Console.WriteLine($"Hello {subject}!");
    }
}

Builder API

using System;
using McMaster.Extensions.CommandLineUtils;

public class Program
{
    public static int Main(string[] args)
    {
        var app = new CommandLineApplication();

        app.HelpOption();
        var optionSubject = app.Option("-s|--subject <SUBJECT>", "The subject", CommandOptionType.SingleValue);

        app.OnExecute(() =>
        {
            var subject = optionSubject.HasValue()
                ? optionSubject.Value()
                : "world";

            Console.WriteLine($"Hello {subject}!");
            return 0;
        });

        return app.Execute(args);
    }
}

Usage

See detailed examples, see the samples for more examples, such as:

CommandLineApplication is the main entry point for most console apps parsing. There are two primary ways to use this API, using the builder pattern and attributes.

Attribute API

using System;
using McMaster.Extensions.CommandLineUtils;

[HelpOption]
public class Program
{
    public static int Main(string[] args)
        => CommandLineApplication.Execute<Program>(args);

    [Option(Description = "The subject")]
    public string Subject { get; }

    private void OnExecute()
    {
        var subject = Subject ?? "world";
        Console.WriteLine($"Hello {subject}!");
    }
}

Builder API

using System;
using McMaster.Extensions.CommandLineUtils;

public class Program
{
    public static int Main(string[] args)
    {
        var app = new CommandLineApplication();

        app.HelpOption();
        var optionSubject = app.Option("-s|--subject <SUBJECT>", "The subject", CommandOptionType.SingleValue);

        app.OnExecute(() =>
        {
            var subject = optionSubject.HasValue()
                ? optionSubject.Value()
                : "world";

            Console.WriteLine($"Hello {subject}!");
            return 0;
        });

        return app.Execute(args);
    }
}

Release Notes

2.1.0:
New features:
- Attributes. Simplify command line argument definitions by adding attributes to a class that represents options and arguments.
   - Options defined as [Option] or [Argument], [Subcommand].
   - Command parsing options can be defined with [Command] and [Subcmomand].
   - Special options include [HelpOption] and [VersionOption].
- Async from end to end. Using C# 7.1 and attribute binding, your console app can be async from top to bottom.

New API
- Added OptionAttribute, ArgumentAttribute, CommandAttribute, SubcommandAttribute, HelpOptionAttribute, and VersionOptionAttribute.
- CommandLineApplication.Execute<TApp>() - executes an app where TApp uses attributes to define its options
- CommandLineApplication.ExecuteAsync<TApp>() - sample thing, but async.
- CommandLineApplication.StopParsingHelpOption and StopParsingVerboseOption. When the help and verbose options are matched
  against a command-line flag, the parsing will stop by default. You can turn this off by setting these options if you
  want OnExecute to be invoked no matter what.
- CommandLineApplication.HandleResponseFiles - the parser can treat arguments that begin with '@' as response files.
  Response files contain arguments that will be treated as if they were passed on command line.

Minor bug fixes:
- Add return types to .VerboseOption() and ensure .HasValue() is true when HelpOption or VerboseOption are matched
- Fix a NullReferenceException in some edge cases when parsing args

Version History

Version Downloads Last updated
2.2.4 2,397 5/25/2018
2.2.3 1,922 5/11/2018
2.2.2 2,947 4/28/2018
2.2.1 3,298 4/11/2018
2.2.0 9,493 3/31/2018
2.2.0-rc 491 3/23/2018
2.2.0-beta 347 3/8/2018
2.2.0-alpha 285 2/20/2018
2.1.1 14,968 12/28/2017
2.1.0 1,042 12/13/2017
2.1.0-rc 237 12/7/2017
2.1.0-beta 550 11/22/2017
2.1.0-alpha (current) 163 11/11/2017
2.0.1 3,037 10/13/2017
2.0.0 2,189 9/16/2017
Show less