McMaster.Extensions.CommandLineUtils 4.1.1
CommandLineUtils
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:
- Hello world
- Async console apps
- Structuring an app with subcommands
- Defining options with attributes
- Interactive console prompts
- Required options and arguments
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 processProcess.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 ...
- read the documentation (https://natemcmaster.github.io/CommandLineUtils/),
- look at the samples (https://github.com/natemcmaster/CommandLineUtils/tree/main/docs/samples),
- review existing questions (many were answered already) (https://github.com/natemcmaster/CommandLineUtils/issues?q=label%3Aquestion+),
- or use a programming Q&A forum such as StackOverflow.com
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).
No packages depend on McMaster.Extensions.CommandLineUtils.
.NET Framework 4.6
- System.Runtime.InteropServices.RuntimeInformation (>= 4.3.0)
- System.ValueTuple (>= 4.5.0)
.NET Standard 2.0
- System.ComponentModel.Annotations (>= 5.0.0)
.NET Standard 2.1
- System.ComponentModel.Annotations (>= 5.0.0)
Version | Downloads | Last updated |
---|---|---|
4.1.1 | 6 | 05/12/2024 |
4.1.0 | 0 | 08/26/2023 |
4.0.2 | 0 | 11/19/2022 |
4.0.1 | 0 | 02/01/2022 |
4.0.0 | 9 | 02/20/2023 |
4.0.0-beta.74 | 5 | 05/18/2024 |
4.0.0-beta.56 | 0 | 01/17/2021 |
3.1.0 | 0 | 01/10/2021 |
3.1.0-rc.371 | 0 | 11/07/2020 |
3.1.0-beta.356 | 5 | 05/15/2024 |
3.1.0-beta.336 | 0 | 08/28/2020 |
3.0.0 | 0 | 03/29/2020 |
3.0.0-rc.289 | 0 | 03/21/2020 |
3.0.0-alpha.268 | 0 | 03/09/2020 |
2.6.0 | 6 | 05/28/2024 |
2.5.1 | 0 | 02/07/2020 |
2.5.0 | 0 | 01/02/2020 |
2.4.4 | 0 | 11/11/2019 |
2.4.3 | 3 | 05/12/2024 |
2.4.2 | 0 | 09/24/2019 |
2.4.1 | 0 | 09/18/2019 |
2.4.0 | 0 | 09/14/2019 |
2.3.4 | 0 | 04/11/2019 |
2.3.3 | 5 | 05/15/2024 |
2.3.2 | 0 | 02/05/2019 |
2.3.1 | 0 | 01/19/2019 |
2.3.0 | 4 | 05/11/2024 |
2.2.5 | 4 | 05/15/2024 |
2.2.4 | 5 | 05/11/2024 |
2.2.3 | 0 | 05/11/2018 |
2.2.2 | 0 | 04/28/2018 |
2.2.1 | 0 | 04/11/2018 |
2.2.0 | 0 | 03/31/2018 |
2.2.0-rc | 0 | 03/23/2018 |
2.2.0-beta | 0 | 03/08/2018 |
2.2.0-alpha | 0 | 02/20/2018 |
2.1.1 | 0 | 12/28/2017 |
2.1.0 | 0 | 12/13/2017 |
2.1.0-rc | 0 | 12/07/2017 |
2.1.0-beta | 0 | 11/22/2017 |
2.1.0-alpha | 0 | 11/11/2017 |
2.0.1 | 0 | 10/13/2017 |
2.0.0 | 0 | 09/16/2017 |