McMaster.Extensions.CommandLineUtils 4.0.0

CommandLineUtils

Build Status Code Coverage NuGet NuGet Downloads

This project helps you create command line applications using .NET. It simplifies parsing arguments provided on the command line, validating user inputs, and generating help text.

The roadmap for this project is pinned to the top of the issue list.

Usage

See documentation for API reference, samples, and tutorials. See also docs/samples/ for more examples, such as:

Installing the library

This project is available as a NuGet package.

$ dotnet add package McMaster.Extensions.CommandLineUtils

Code

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; } = "world";

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

    private void OnExecute()
    {
        for (var i = 0; i < Count; i++)
        {
            Console.WriteLine($"Hello {Subject}!");
        }
    }
}

Builder API

using System;
using McMaster.Extensions.CommandLineUtils;

var app = new CommandLineApplication();

app.HelpOption();

var subject = app.Option("-s|--subject <SUBJECT>", "The subject", CommandOptionType.SingleValue);
subject.DefaultValue = "world";

var repeat = app.Option<int>("-n|--count <N>", "Repeat", CommandOptionType.SingleValue);
repeat.DefaultValue = 1;

app.OnExecute(() =>
{
    for (var i = 0; i < repeat.ParsedValue; i++)
    {
        Console.WriteLine($"Hello {subject.Value()}!");
    }
});

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 with a default answer. A few examples:
    // allows y/n responses, will return false by default in this case.
    // You may optionally change the prompt foreground and background color for
    // the message.
    Prompt.GetYesNo("Do you want to proceed?", false);
    
    // 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.

Project origin and status

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 accept contributions. It is currently maintained by @natemcmaster.

Showing the top 20 packages that depend on McMaster.Extensions.CommandLineUtils.

Packages Downloads
dotnet-serve
A simple command-line HTTP server.
19

Features: * @scott-xu and @natemcmaster: feature: add API for setting default value on options and arguments, and display them in help text (#389 and #420) * @natemcmaster: cleanup: make .Values read-only on CommandArgument/Option (#406) * @natemcmaster: Make options, arguments, and commands read-only collections on CommandLineApplication (#407) Fixes: * @natemcmaster: fix: pass the generic argument version in callback on .Option() and Argument() (#405) * @natemcmaster: add .NET Standard 2.1 and fix nullable type references (#424 and #425) * @natemcmaster: fix new CI to correctly publish symbols to nuget.org * @scott-xu: show option types in help text when OptionAttribute.Template is set (#429) * @skirchner989: change to not throw when a validator is not of type AttributeValidator (#431) * @natemcmaster: don't mask OperationCanceledException triggered by SIGINT (#483) See more details here: https://github.com/natemcmaster/CommandLineUtils/blob/main/CHANGELOG.md#v400

.NET Framework 4.5

.NET Standard 2.0

.NET Standard 2.1

Version Downloads Last updated
4.1.1 26 04/18/2024
4.1.0 23 02/15/2024
4.0.2 21 02/15/2024
4.0.1 22 03/03/2022
4.0.0 23 02/15/2024
4.0.0-beta.74 22 02/07/2024
4.0.0-beta.56 20 02/07/2024
3.1.0 21 02/15/2024
3.1.0-rc.371 23 02/07/2024
3.1.0-beta.356 22 02/07/2024
3.1.0-beta.336 21 02/07/2024
3.0.0 16 02/15/2024
3.0.0-rc.289 23 02/07/2024
3.0.0-alpha.268 19 02/07/2024
2.6.0 23 02/15/2024
2.5.1 23 02/15/2024
2.5.0 24 02/15/2024
2.4.4 25 02/15/2024
2.4.3 22 02/15/2024
2.4.2 24 02/15/2024
2.4.1 21 02/15/2024
2.4.0 22 02/15/2024
2.3.4 27 02/15/2024
2.3.3 20 02/15/2024
2.3.2 22 02/15/2024
2.3.1 21 02/15/2024
2.3.0 17 02/15/2024
2.2.5 24 02/15/2024
2.2.4 22 02/15/2024
2.2.3 22 02/15/2024
2.2.2 20 02/15/2024
2.2.1 24 02/15/2024
2.2.0 23 02/15/2024
2.2.0-rc 22 02/07/2024
2.2.0-beta 25 02/07/2024
2.2.0-alpha 21 02/07/2024
2.1.1 22 02/15/2024
2.1.0 23 02/15/2024
2.1.0-rc 22 02/07/2024
2.1.0-beta 20 02/07/2024
2.1.0-alpha 21 02/07/2024
2.0.1 23 02/15/2024
2.0.0 15 02/15/2024