Skip to content

tomexou/CommandLineUtils

 
 

Repository files navigation

CommandLineUtils

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 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.1.0" />
</ItemGroup>

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

Usage

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;

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

About

Command line parsing and utilities for .NET Core and .NET Framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.2%
  • Other 0.8%