McMaster.Extensions.CommandLineUtils 4.0.1 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved

.NET Standard 2.0 .NET Framework 4.5
Install-Package McMaster.Extensions.CommandLineUtils -Version 4.0.1
dotnet add package McMaster.Extensions.CommandLineUtils --version 4.0.1
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.1" />
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.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: McMaster.Extensions.CommandLineUtils, 4.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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.1

// Install McMaster.Extensions.CommandLineUtils as a Cake Tool
#tool nuget:?package=McMaster.Extensions.CommandLineUtils&version=4.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

Getting help

If you need help with this project, please ...

Project origin and status

This is a fork of Microsoft.Extensions.CommandLineUtils, which was completely abandoned by Microsoft. This project forked in 2017 and continued to make improvements. From 2017 to 2021, over 30 contributors added new features and fixed bugs. As of 2022, the project has entered maintenance mode, so no major changes are planned. See this issue for details on latest project status. This project is not abandoned -- I believe this library provides a stable API and rich feature set good enough for most developers to create command line apps in .NET -- but only the most critical of bugs will be fixed (such as security issues).

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

NuGet packages (62)

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).

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

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

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.

Rocket.Surgery.Extensions.CommandLine.Abstractions

Provides the ability to provide additional commands to the hosted application by convention

GitHub repositories (87)

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

Repository Stars
icsharpcode/ILSpy
.NET Decompiler with support for PDB generation, ReadyToRun, Metadata (&more) - cross-platform!
JeffreySu/WeiXinMPSDK
微信全平台 SDK Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 6.0。已支持微信公众号、小程序、小游戏、企业号、企业微信、开放平台、微信支付、JSSDK、微信周边等全平台。 WeChat SDK for C#.
QuantConnect/Lean
Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
ChilliCream/hotchocolate
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 the .NET Framework.
Version Downloads Last updated
4.0.1 179,465 2/1/2022
4.0.0 141,920 12/27/2021
4.0.0-beta.74 30,953 1/22/2021
4.0.0-beta.56 814 1/17/2021
3.1.0 1,343,609 1/10/2021
3.1.0-rc.371 3,639 11/7/2020
3.1.0-beta.356 1,189 9/13/2020
3.1.0-beta.336 435 8/28/2020
3.0.0 1,628,196 3/29/2020
3.0.0-rc.289 4,764 3/21/2020
3.0.0-alpha.268 2,247 3/9/2020
2.6.0 152,946 3/8/2020
2.5.1 169,712 2/7/2020
2.5.0 161,249 1/2/2020
2.4.4 138,681 11/11/2019
2.4.3 25,058 11/1/2019
2.4.2 121,213 9/24/2019
2.4.1 14,164 9/18/2019
2.4.0 13,766 9/14/2019
2.3.4 770,825 4/11/2019
2.3.3 139,465 3/11/2019
2.3.2 273,546 2/5/2019
2.3.1 34,218 1/19/2019
2.3.0 171,459 1/1/2019
2.2.5 748,732 7/2/2018
2.2.4 91,388 5/25/2018
2.2.3 23,780 5/11/2018
2.2.2 56,052 4/28/2018
2.2.1 69,741 4/11/2018
2.2.0 292,892 3/31/2018
2.2.0-rc 2,682 3/23/2018
2.2.0-beta 1,232 3/8/2018
2.2.0-alpha 1,176 2/20/2018
2.1.1 110,321 12/28/2017
2.1.0 6,524 12/13/2017
2.1.0-rc 1,156 12/7/2017
2.1.0-beta 2,053 11/22/2017
2.1.0-alpha 1,015 11/11/2017
2.0.1 8,482 10/13/2017
2.0.0 7,956 9/16/2017

Changes since 3.1.0:

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)

Updates in 4.0.1 patch:
* @xoofx: use explicit constructors on validation attributes to workaround .NET 6 runtime trimming error (#491)
* @jakubqwe: support the Description field of VersionOptionFromMemberAttribute (#490)
   

See more details here: https://github.com/natemcmaster/CommandLineUtils/blob/main/CHANGELOG.md#v401