SystemCommandLine.Extensions 2.0.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package SystemCommandLine.Extensions --version 2.0.0
                    
NuGet\Install-Package SystemCommandLine.Extensions -Version 2.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="SystemCommandLine.Extensions" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SystemCommandLine.Extensions" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SystemCommandLine.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SystemCommandLine.Extensions --version 2.0.0
                    
#r "nuget: SystemCommandLine.Extensions, 2.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.
#:package SystemCommandLine.Extensions@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SystemCommandLine.Extensions&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SystemCommandLine.Extensions&version=2.0.0
                    
Install as a Cake Tool

SystemCommandLine.Extensions

Build NuGet NuGet Downloads

Small, pragmatic helpers for building command-line apps with System.CommandLine and the Microsoft.Extensions hosting/DI stack.

  • Fluent builders to define options from a typed options class using expressions
  • Automatic mapping from parsed command-line values to your options (IOptions<T>)
  • DI-first wiring for commands and handlers
  • Host-friendly RootCommand setup and ParseResult lifetime integration
  • Kebab-case option names by convention (e.g., MyProperty → --my-property)

Install

dotnet add package SystemCommandLine.Extensions

Quick start

Define a command and its options using the builder API and expressions. Implement IUseCommandBuilder<TCommand> to expose a factory that the DI extensions can use.

using System.CommandLine;
using SystemCommandLine.Extensions;

public class Greet : Command, IUseCommandBuilder<Greet>
{
	public Greet(ArgumentMapperRegistration mapperRegistration)
		: base("greet", "Greets a person")
	{
		// Describe options using your options type and expressions
		this.UseCommandBuilder().WithMapper<GreetOptions>(mapperRegistration)
			.NewOption(o => o.Name).Configure(o =>
			{
				o.Description = "Name to greet";
				o.DefaultValueFactory = _ => "World";
			}).AddToCommand()
			.NewOption(o => o.Times).Configure(o =>
			{
				o.Description = "Number of times";
				o.DefaultValueFactory = _ => 1;
			}).AddToCommand();
	}

	public static Greet CommandFactory(IServiceProvider sp, ArgumentMapperRegistration mapperRegistration)
		=> new(mapperRegistration);
}

public class GreetOptions
{
	public string Name { get; set; }
	public int Times { get; set; }
}

Wire it up with hosting and DI. The parsed values are bound into IOptions<GreetOptions>.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.CommandLine;
using SystemCommandLine.Extensions;

var builder = Host.CreateDefaultBuilder(args)
	.ConfigureServices(services =>
	{
		// Root and command registration
		services.AddRootCommand<Root>(args);
		services.AddCommandWithAsyncHandler<Greet, GreetHandler>();

		// Bind parsed command options into IOptions<T>
		services.AddBoundToCommandOptions<Greet, GreetOptions>();
	});

using IHost host = builder.Start();
ParseResult parse = host.Services.GetRequiredService<ParseResult>();
return await parse.InvokeAsync();

See src/TestConsoleApp for a complete example, including logging integration.

Why use this?

System.CommandLine is powerful but low-level. This library smooths out the most common patterns in real apps:

  • Define options once on a POCO and keep them strongly typed end-to-end
  • Avoid repetitive glue when mapping parsed values into IOptions<T>
  • Compose commands and handlers via DI in a host-friendly way

Requirements

  • .NET 8.0+
  • System.CommandLine 2.0.0

License

MIT. See LICENSE.txt.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.1-isubcommandof-preview.1 144 11/23/2025
2.0.0 308 11/11/2025
2.0.0-rc.2.25502.107.1 146 10/14/2025
2.0.0-rc.1.25451.107.3 181 9/20/2025
2.0.0-rc.1.25451.107.2 272 9/17/2025
2.0.0-rc.1.25451.107.1 140 9/10/2025
2.0.0-beta7.25380.108.3 150 9/7/2025
2.0.0-beta7.25380.108.1 69 8/23/2025
2.0.0-beta7.25380.108 66 8/23/2025