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.
The roadmap for this project can be found here.
Install the NuGet package into your project.
PM> Install-Package McMaster.Extensions.CommandLineUtils
$ dotnet add package McMaster.Extensions.CommandLineUtils
See documentation for API reference, samples, and tutorials. See samples/ for more examples, such as:
- Async console apps
- Structing an app with subcommands
- Defining options with attributes
- Interactive console prompts
- Required options and arguments
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.
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}!");
}
}
}
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);
}
}
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 processProcess.Start(DotNetExe.FullPathOrDefault(), "run");
And more! See the documentation for more API, such as IConsole
, IReporter
, and others.