![]() |
VOOZH | about |
dotnet add package System.CommandLine --version 2.0.9
NuGet\Install-Package System.CommandLine -Version 2.0.9
<PackageReference Include="System.CommandLine" Version="2.0.9" />
<PackageVersion Include="System.CommandLine" Version="2.0.9" />Directory.Packages.props
<PackageReference Include="System.CommandLine" />Project file
paket add System.CommandLine --version 2.0.9
#r "nuget: System.CommandLine, 2.0.9"
#:package System.CommandLine@2.0.9
#addin nuget:?package=System.CommandLine&version=2.0.9Install as a Cake Addin
#tool nuget:?package=System.CommandLine&version=2.0.9Install as a Cake Tool
System.CommandLine provides robust support for command-line parsing, invocation, and shell completions in .NET applications. It supports both POSIX and Windows conventions, making it easy to build professional command-line interfaces.
Here's a simple "Hello World" command-line application:
using System.CommandLine;
RootCommand rootCommand = new("Sample command-line app");
Option<string> nameOption = new("--name", "-n")
{
Description = "Your name"
};
rootCommand.Options.Add(nameOption);
rootCommand.SetAction(parseResult =>
{
string name = parseResult.GetValue(nameOption);
Console.WriteLine($"Hello, {name ?? "World"}!");
});
return rootCommand.Parse(args).Invoke();
In this example, we create a RootCommand, add an option for the user's name, and define an action that prints a greeting. The RootCommand is a special kind of Command that comes with a few predefined behaviors:
--help and --version options and default behaviors for themdotnet-suggest for dynamic shell completionsYou can always override or customize these behaviors as needed on a RootCommand, or create your own top-level Command instead.
Arguments are values passed directly to commands without option names:
var fileArgument = new Argument<FileInfo>("file")
{
Description = "The file to process"
};
var processCommand = new Command("process", "Process a file");
processCommand.Arguments.Add(fileArgument);
processCommand.SetAction(parseResult =>
{
FileInfo file = parseResult.GetValue(fileArgument);
Console.WriteLine($"Processing {file.FullName}");
});
var rootCommand = new RootCommand();
rootCommand.Subcommands.Add(processCommand);
Options can have default values and validation:
var rootCommand = new RootCommand();
var delayOption = new Option<int>("--delay", "-d")
{
Description = "Delay in milliseconds",
DefaultValueFactory = _ => 1000
};
delayOption.Validators.Add(result =>
{
if (result.GetValueOrDefault<int>() < 0)
{
result.AddError("Delay must be non-negative");
}
});
rootCommand.Options.Add(delayOption);
Build complex CLI applications with nested commands:
var rootCommand = new RootCommand("My application");
var configCommand = new Command("config", "Configure the application");
var configSetCommand = new Command("set", "Set a configuration value");
var configGetCommand = new Command("get", "Get a configuration value");
var keyOption = new Option<string>("--key")
{
Description = "Configuration key"
};
var valueOption = new Option<string>("--value")
{
Description = "Configuration value"
};
configSetCommand.Options.Add(keyOption);
configSetCommand.Options.Add(valueOption);
configGetCommand.Options.Add(keyOption);
configCommand.Subcommands.Add(configSetCommand);
configCommand.Subcommands.Add(configGetCommand);
rootCommand.Subcommands.Add(configCommand);
// Usage: myapp config set --key "apiUrl" --value "https://api.example.com"
// Usage: myapp config get --key "apiUrl"
Access option values through the ParseResult:
var connectionOption = new Option<string>("--connection")
{
Description = "Database connection string"
};
var timeoutOption = new Option<int>("--timeout")
{
Description = "Timeout in seconds",
DefaultValueFactory = _ => 30
};
var verboseOption = new Option<bool>("--verbose")
{
Description = "Enable verbose output"
};
rootCommand.Options.Add(connectionOption);
rootCommand.Options.Add(timeoutOption);
rootCommand.Options.Add(verboseOption);
rootCommand.SetAction(parseResult =>
{
var connection = parseResult.GetValue(connectionOption);
var timeout = parseResult.GetValue(timeoutOption);
var verbose = parseResult.GetValue(verboseOption);
Console.WriteLine($"Connection: {connection}");
Console.WriteLine($"Timeout: {timeout}");
Console.WriteLine($"Verbose: {verbose}");
});
Enable tab completion for your CLI:
// Completions are automatically available for all commands, options, and arguments
var rootCommand = new RootCommand("My app with completions");
var fileOption = new Option<FileInfo>("--file")
{
Description = "The file to process"
};
// Add custom completions using CompletionSources
fileOption.CompletionSources.Add(ctx =>
// hard-coded list of files
["file1.txt", "file2.txt", "file3.txt" ]
);
// Or add simple string suggestions
fileOption.CompletionSources.Add("option1", "option2", "option3");
rootCommand.Options.Add(fileOption);
Users can then easily trigger your completions using dotnet-suggest:
> dotnet tool install -g dotnet-suggest
> dotnet suggest script bash > ~/.bashrc
> dotnet tool install -g dotnet-suggest
> dotnet suggest script powershell >> $PROFILE
Once dotnet-suggest is installed, you can register your app with it for completions support:
> dotnet-suggest register --command-path /path/to/myapp
Alternatively, you can create your own commands for completion generation and instruct users on how to set them up.
Support for asynchronous operations:
var urlOption = new Option<string>("--url")
{
Description = "The URL to fetch"
};
rootCommand.Options.Add(urlOption);
rootCommand.SetAction(async (parseResult, cancellationToken) =>
{
var url = parseResult.GetValue(urlOption);
if (url != null)
{
using var client = new HttpClient();
var response = await client.GetStringAsync(url, cancellationToken);
Console.WriteLine(response);
}
});
// Or return an exit code:
rootCommand.SetAction(async (parseResult, cancellationToken) =>
{
// Your async logic here
return await Task.FromResult(0); // Return exit code
});
HelpAction to allow users to provide custom MaxWidth for help text formatting (#2635). Note that if you create custom Help or Version actions, you'll want to set ClearsParseErrors to true to ensure that invoking those features isn't treated like an error by the parser.Task<int> Support: Added SetAction overload for Task<int> return types (#2634)ArgumentResult.Implicit property for better argument handling (#2622, #2625)ProcessTerminationTimeout has been re-added (#2672)For comprehensive documentation, tutorials, and API reference, visit:
This package is licensed under the MIT License.
We welcome contributions! Please see our Contributing Guide for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on System.CommandLine:
| Package | Downloads |
|---|---|
|
System.CommandLine.NamingConventionBinder
This package provides command handler support for System.CommandLine performs parameter and model binding by matching option and argument names to parameter and property names. |
|
|
System.CommandLine.Rendering
This package provides support for structured command line output rendering. Write code once that renders correctly in multiple output modes, including System.Console, virtual terminal (using ANSI escape sequences), and plain text. |
|
|
System.CommandLine.DragonFruit
This package includes the experimental DragonFruit app model for System.CommandLine, which allows you to create a command line application using only a Main method while getting support for complex type binding, error reporting, help, shell completions, and more. |
|
|
System.CommandLine.Hosting
This package provides support for using System.CommandLine with Microsoft.Extensions.Hosting. |
|
|
HotChocolate.AspNetCore.CommandLine
This package contains the command line extensions for HotChocolate |
Showing the top 20 popular GitHub repositories that depend on System.CommandLine:
| Repository | Stars |
|---|---|
|
PowerShell/PowerShell
PowerShell for every system!
|
|
|
dotnet/aspnetcore
ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
|
|
|
DevToys-app/DevToys
A Swiss Army knife for developers.
|
|
|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
|
BeyondDimension/SteamTools
🛠「Watt Toolkit」是一个开源跨平台的多功能 Steam 工具箱。
|
|
|
dotnet/roslyn
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
|
|
|
dotnet/runtime
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
|
|
|
MaterialDesignInXAML/MaterialDesignInXamlToolkit
Google's Material Design in XAML & WPF, for C# & VB.Net.
|
|
|
dotnet/efcore
EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
|
|
|
duplicati/duplicati
Store securely encrypted backups in the cloud!
|
|
|
winsw/winsw
A wrapper executable that can run any executable as a Windows service, in a permissive license.
|
|
|
MonoGame/MonoGame
One framework for creating powerful cross-platform games.
|
|
|
nilaoda/BBDown
Bilibili Downloader. 一个命令行式哔哩哔哩下载器.
|
|
| MiniMax-AI/skills | |
|
gui-cs/Terminal.Gui
Cross Platform Terminal UI toolkit for .NET
|
|
|
dotnet/orleans
Cloud Native application framework for .NET
|
|
|
studyzy/imewlconverter
”深蓝词库转换“ 一款开源免费的输入法词库转换程序
|
|
|
unoplatform/uno
Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
|
|
|
git-ecosystem/git-credential-manager
Secure, cross-platform Git credential storage with authentication to GitHub, Azure Repos, and other popular Git hosting services.
|
|
|
nilaoda/N_m3u8DL-RE
Cross-Platform, modern and powerful stream downloader for MPD/M3U8/ISM. English/简体中文/繁體中文.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.0-preview.5.26302.115 | 2,588 | 6/9/2026 |
| 3.0.0-preview.4.26230.115 | 15,224 | 5/12/2026 |
| 3.0.0-preview.3.26207.106 | 16,394 | 4/14/2026 |
| 3.0.0-preview.2.26159.112 | 29,335 | 3/10/2026 |
| 3.0.0-preview.1.26104.118 | 13,499 | 2/10/2026 |
| 2.0.9 | 131,221 | 6/9/2026 |
| 2.0.8 | 641,865 | 5/12/2026 |
| 2.0.7 | 663,952 | 4/21/2026 |
| 2.0.6 | 245,071 | 4/14/2026 |
| 2.0.5 | 1,262,944 | 3/12/2026 |
| 2.0.4 | 163,422 | 3/10/2026 |
| 2.0.3 | 842,821 | 2/10/2026 |
| 2.0.2 | 735,225 | 1/13/2026 |
| 2.0.1 | 1,325,853 | 12/9/2025 |
| 2.0.0 | 1,386,552 | 11/11/2025 |
| 2.0.0-rc.2.25502.107 | 495,824 | 10/14/2025 |
| 2.0.0-rc.1.25451.107 | 525,359 | 9/9/2025 |
| 2.0.0-beta7.25380.108 | 386,985 | 8/12/2025 |
| 2.0.0-beta6.25358.103 | 291,813 | 7/15/2025 |
| 2.0.0-beta5.25306.1 | 634,353 | 6/19/2025 |