McMaster.Extensions.CommandLineUtils 2.2.4

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

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

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;

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);
    }
}

Utilities

The library also includes other utilities for interaction with the console. These include:

  • ArgumentEscaper - use to escape arguments when starting a new command line process.
     var args = new [] { "Arg1", "arg with space", "args ' with \" quotes" };
     Process.Start("echo", ArgumentEscaper.EscapeAndConcatenate(args));
    
  • Prompt - for getting feedback from users. A few examples:
    // allows y/n responses
    Prompt.GetYesNo("Do you want to proceed?");
    
    // masks input as '*'
    Prompt.GetPassword("Password: ");
    
  • DotNetExe - finds the path to the dotnet.exe file used to start a .NET Core process
    Process.Start(DotNetExe.FullPathOrDefault(), "run");
    

And more! See the docs for more API, such as IConsole, IReporter, and others.

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;

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);
    }
}

Utilities

The library also includes other utilities for interaction with the console. These include:

  • ArgumentEscaper - use to escape arguments when starting a new command line process.
     var args = new [] { "Arg1", "arg with space", "args ' with \" quotes" };
     Process.Start("echo", ArgumentEscaper.EscapeAndConcatenate(args));
    
  • Prompt - for getting feedback from users. A few examples:
    // allows y/n responses
    Prompt.GetYesNo("Do you want to proceed?");
    
    // masks input as '*'
    Prompt.GetPassword("Password: ");
    
  • DotNetExe - finds the path to the dotnet.exe file used to start a .NET Core process
    Process.Start(DotNetExe.FullPathOrDefault(), "run");
    

And more! See the docs for more API, such as IConsole, IReporter, and others.

Release Notes

Bug fixes:

* @liamdawson: support parsing `System.Uri`
* Fix #101 - Update `DotNetExe.FullPath` to honor the `DOTNET_ROOT` environment variable as a fallback

See more details here: https://github.com/natemcmaster/CommandLineUtils/blob/master/CHANGELOG.md#v224

Version History

Version Downloads Last updated
2.3.2 3,275 2/5/2019
2.3.1 4,844 1/19/2019
2.3.0 14,278 1/1/2019
2.3.0-rc.460 214 12/15/2018
2.3.0-beta 551 12/4/2018
2.3.0-alpha 1,189 10/3/2018
2.2.5 124,489 7/2/2018
2.2.4 26,283 5/25/2018
2.2.3 15,120 5/11/2018
2.2.2 8,021 4/28/2018
2.2.1 7,733 4/11/2018
2.2.0 69,373 3/31/2018
2.2.0-rc 733 3/23/2018
2.2.0-beta 452 3/8/2018
2.2.0-alpha 437 2/20/2018
2.1.1 35,175 12/28/2017
2.1.0 1,457 12/13/2017
2.1.0-rc 406 12/7/2017
2.1.0-beta 1,050 11/22/2017
2.1.0-alpha 299 11/11/2017
2.0.1 4,734 10/13/2017
2.0.0 3,303 9/16/2017
Show less