Command line parsing and utilities for .NET Core and .NET Framework.
Clone or download
Latest commit 1e9bdde Oct 18, 2018
Permalink
Failed to load latest commit information.
.github Create no-response.yml Sep 28, 2018
docs Fix #132 - update documentation on options Oct 18, 2018
src Fix #163 - obsolete CommandOption.Template and fix help text generati… Oct 18, 2018
test/CommandLineUtils.Tests Fix #163 - obsolete CommandOption.Template and fix help text generati… Oct 18, 2018
.appveyor.yml Bump version to 2.3.0-alpha1 Jul 5, 2018
.editorconfig Update .editorconfig Jan 27, 2018
.gitattributes Change the build to use KoreBuild-dotnet Dec 21, 2015
.gitignore Automate documentation generation and move generated content to the g… Apr 21, 2018
.travis.yml Update samples and tests to .NET Core 2.1 May 31, 2018
CHANGELOG.md Fix #163 - obsolete CommandOption.Template and fix help text generati… Oct 18, 2018
CONTRIBUTING.md Automate documentation generation and move generated content to the g… Apr 21, 2018
CommandLineUtils.sln Reorganize samples into the docs folder Jul 6, 2018
CommandLineUtils.sln.licenseheader Expand validation API Dec 31, 2017
Directory.Build.props Switch to Microsoft.SourceLink.GitHub Aug 17, 2018
Directory.Build.targets Implement authenticode signing and package signing Jul 2, 2018
LICENSE.txt Update License.txt and more build cleanup stuff Sep 10, 2017
NOTICE.txt Add NOTICE.txt and update license headers. Sep 3, 2017
NuGet.config Add package reference to .NET Framework ref assemblies for x-plat work Aug 15, 2018
README.md Update Readme reference to include link to docs (#142) Aug 27, 2018
build.cmd Update bootstrappers to use the compiled version of KoreBuild Jul 25, 2017
build.ps1 Fix #160 - upgrade to AzureSignTool 2.0.17 Oct 18, 2018
releasenotes.props Include `UnrecognizedCommandParsingException` in the changelog and re… Oct 16, 2018
version.props Update version to 2.3.0-beta Oct 3, 2018

README.md

CommandLineUtils

Travis build status AppVeyor build status

NuGet MyGet

This is a fork of Microsoft.Extensions.CommandLineUtils, which is no longer under active development. This fork, on the other hand, will continue to make improvements, release updates and take contributions.

Install

Install the NuGet package into your project.

PM> Install-Package McMaster.Extensions.CommandLineUtils
$ dotnet add package McMaster.Extensions.CommandLineUtils
<ItemGroup>
  <PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.5" />
</ItemGroup>

Pre-release builds and symbols: https://www.myget.org/gallery/natemcmaster/

Usage

See documentation for API reference, samples, and tutorials. See 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;

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

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

    [Option(ShortName = "n")]
    public int Count { get; }

    private void OnExecute()
    {
        var subject = Subject ?? "world";
        for (var i = 0; i < Count; i++)
        {
            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);
        var optionRepeat = app.Option<int>("-n|--count <N>", "Repeat", CommandOptionType.SingleValue);

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

            var count = optionRepeat.HasValue() ? optionRepeat.ParsedValue : 1;
            for (var i = 0; i < count; i++)
            {
                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 documentation for more API, such as IConsole, IReporter, and others.