McMaster.Extensions.CommandLineUtils 2.2.3

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.3
dotnet add package McMaster.Extensions.CommandLineUtils --version 2.2.3
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add McMaster.Extensions.CommandLineUtils --version 2.2.3
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

Minor improvement:

* Make it easier to override the version text shown when `--version` is specified.
* Make DefaultHelpTextGenerator's constructor protected so you can inherit from this type
* Fix DebugHelper.HandleDebugSwitch

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

Showing the top 10 GitHub repositories that depend on McMaster.Extensions.CommandLineUtils:

Repository Stars
icsharpcode/ILSpy
.NET Decompiler
IdentityServer/IdentityServer4
OpenID Connect and OAuth 2.0 Framework for ASP.NET Core
dotnet/BenchmarkDotNet
Powerful .NET library for benchmarking
ravendb/ravendb
ACID Document Database
xunit/xunit
xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework.
scriptcs/scriptcs
Write C# apps with a text editor, nuget and the power of Roslyn!
fluentmigrator/fluentmigrator
Fluent migrations framework for .NET
MichaCo/CacheManager
CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
Azure/azure-sdk-for-net
Microsoft Azure SDK for .NET
tonerdo/coverlet
Cross platform code coverage for .NET

Version History

Version Downloads Last updated
2.4.2 6,503 9/24/2019
2.4.1 3,423 9/18/2019
2.4.0 1,923 9/14/2019
2.3.4 118,452 4/11/2019
2.3.3 44,003 3/11/2019
2.3.2 63,679 2/5/2019
2.3.1 18,437 1/19/2019
2.3.0 53,380 1/1/2019
2.2.5 234,948 7/2/2018
2.2.4 42,348 5/25/2018
2.2.3 19,209 5/11/2018
2.2.2 23,702 4/28/2018
2.2.1 9,938 4/11/2018
2.2.0 116,272 3/31/2018
2.2.0-rc 826 3/23/2018
2.2.0-beta 564 3/8/2018
2.2.0-alpha 536 2/20/2018
2.1.1 58,024 12/28/2017
2.1.0 2,028 12/13/2017
2.1.0-rc 511 12/7/2017
2.1.0-beta 1,474 11/22/2017
2.1.0-alpha 422 11/11/2017
2.0.1 5,755 10/13/2017
2.0.0 4,673 9/16/2017
Show less