McMaster.Extensions.CommandLineUtils 4.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package McMaster.Extensions.CommandLineUtils --version 4.0.0
NuGet\Install-Package McMaster.Extensions.CommandLineUtils -Version 4.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add McMaster.Extensions.CommandLineUtils --version 4.0.0
#r "nuget: McMaster.Extensions.CommandLineUtils, 4.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install McMaster.Extensions.CommandLineUtils as a Cake Addin
#addin nuget:?package=McMaster.Extensions.CommandLineUtils&version=4.0.0

// Install McMaster.Extensions.CommandLineUtils as a Cake Tool
#tool nuget:?package=McMaster.Extensions.CommandLineUtils&version=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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (72)

Showing the top 5 NuGet packages that depend on McMaster.Extensions.CommandLineUtils:

Package Downloads
McMaster.Extensions.Hosting.CommandLine The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Provides command-line parsing API integration with the generic host API (Microsoft.Extensions.Hosting).

QuantConnect.Configuration The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

QuantConnect LEAN Engine: Configuration Project - The Config and argument parser implementation

FlubuCore

A cross platform build and deployment automation system for building projects and executing deployment scripts using C# code. Documentation can be found at: https://github.com/dotnetcore/FlubuCore Detailed examples can be found at: https://github.com/dotnetcore/FlubuCore.Examples

KubeOps

This is an operator sdk written in c#. It enables a developer to create a custom controller for CRDs (CustomResourceDefinitions) that runs on kubernetes.

Microsoft.Jupyter.Core The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Provides support for writing Jupyter kernels using the .NET Core SDK.

GitHub repositories (102)

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

Repository Stars
QuantConnect/Lean
Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
ChilliCream/graphql-platform
Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
xunit/xunit
xUnit.net is a free, open source, community-focused unit testing tool for .NET.
cobbr/Covenant
Covenant is a collaborative .NET C2 framework for red teamers.
jamesmh/coravel
Near-zero config .NET library that makes advanced application features like Task Scheduling, Caching, Queuing, Event Broadcasting, and more a breeze!
Version Downloads Last updated
4.1.1 126,458 2/19/2024
4.1.0 545,570 8/26/2023
4.0.2 1,317,300 11/19/2022
4.0.1 1,831,143 2/1/2022
4.0.0 570,719 12/27/2021
4.0.0-beta.74 47,047 1/22/2021
4.0.0-beta.56 1,175 1/17/2021
3.1.0 2,517,512 1/10/2021
3.1.0-rc.371 5,529 11/7/2020
3.1.0-beta.356 1,319 9/13/2020
3.1.0-beta.336 532 8/28/2020
3.0.0 2,449,330 3/29/2020
3.0.0-rc.289 4,900 3/21/2020
3.0.0-alpha.268 2,445 3/9/2020
2.6.0 627,266 3/8/2020
2.5.1 229,788 2/7/2020
2.5.0 233,639 1/2/2020
2.4.4 191,424 11/11/2019
2.4.3 28,135 11/1/2019
2.4.2 170,174 9/24/2019
2.4.1 19,736 9/18/2019
2.4.0 17,414 9/14/2019
2.3.4 984,083 4/11/2019
2.3.3 199,177 3/11/2019
2.3.2 306,566 2/5/2019
2.3.1 43,171 1/19/2019
2.3.0 247,121 1/1/2019
2.2.5 1,265,047 7/2/2018
2.2.4 236,381 5/25/2018
2.2.3 30,508 5/11/2018
2.2.2 85,055 4/28/2018
2.2.1 103,109 4/11/2018
2.2.0 495,620 3/31/2018
2.2.0-rc 7,934 3/23/2018
2.2.0-beta 3,087 3/8/2018
2.2.0-alpha 3,045 2/20/2018
2.1.1 126,860 12/28/2017
2.1.0 12,852 12/13/2017
2.1.0-rc 2,975 12/7/2017
2.1.0-beta 3,900 11/22/2017
2.1.0-alpha 2,800 11/11/2017
2.0.1 16,856 10/13/2017
2.0.0 14,638 9/16/2017

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