Spectre.Console.Cli Documentation
Build powerful command-line applications with Spectre.Console.Cli, a modern framework with a focus on type safety and developer experience.
Start Here
Get Spectre.Console.Cli running in seconds:
dotnet add package Spectre.Console.Cli
Then try this quick example that creates a simple greeting command:
public class GreetSettings : CommandSettings
{
[CommandArgument(0, "<name>")]
[Description("The name to greet")]
public required string Name { get; init; }
[CommandOption("-c|--count")]
[Description("Number of times to greet")]
[DefaultValue(1)]
public int Count { get; init; }
}
public class GreetCommand : Command<GreetSettings>
{
protected override int Execute(CommandContext context, GreetSettings settings, CancellationToken cancellation)
{
for (var i = 0; i < settings.Count; i++)
{
AnsiConsole.MarkupLine($"Hello, [green]{settings.Name}[/]!");
}
return 0;
}
}
In your Program.cs, wire it up:
var app = new CommandApp<GreetCommand>();
return app.Run(args);
Run it with dotnet run -- World --count 3 to see typed argument parsing in action.
You should see something like this:
Explore the Documentation
From simple single-command tools to complex CLI applications with nested commands and dependency injection, we've got you covered. The tutorials will walk you through the fundamentals, while the how-to guides tackle specific scenarios you'll encounter in real projects.
Tutorials
- Quick Start: Your First CLI App - Build your first command-line application with Spectre.Console.Cli
- Building a Multi-Command CLI Tool - Build a CLI application with multiple commands, subcommands, and shared settings
- Dependency Injection in CLI Apps - Inject services into your CLI commands using Microsoft.Extensions.DependencyInjection
- Logging in CLI Apps - Add structured logging to your CLI commands using Microsoft.Extensions.Logging
How-To Guides
- Defining Commands and Arguments - How to declare command-line parameters (arguments and options) using Spectre.Console.Cli's attributes and settings classes
- Making Options Required - How to make command-line options required instead of optional in Spectre.Console.Cli
- Handling Errors and Exit Codes - How Spectre.Console.Cli deals with exceptions and how to customize error handling
- Async Commands and Cancellation - How to create asynchronous commands and handle cancellation in Spectre.Console.Cli
- Configuring CommandApp and Commands - How to register commands with the CommandApp and configure global settings
- Customizing Help Text and Usage - How to tailor the automatically generated help output of Spectre.Console.Cli
- Working with Multiple Command Hierarchies - How to create hierarchical (nested) commands using branching
- Using Flag Arguments - How to use FlagValue for optional flag arguments that may or may not include a value
- Using Dictionary and Lookup Options - How to accept key-value pairs using IDictionary, ILookup, and IReadOnlyDictionary options
- Using Custom Type Converters - How to create and apply custom type converters for complex command-line argument types
- Testing Command-Line Applications - How to test CLI apps built with Spectre.Console.Cli to ensure they parse and execute correctly
- Hiding Commands and Options - How to hide commands and options from help output while keeping them functional
- Intercepting Command Execution - How to use command interceptors to run logic before or after any command executes
Explanation
- Design Philosophy: Convention over Configuration - An explanation of the guiding philosophy behind Spectre.Console.Cli
- Command Lifecycle and Execution Flow - An explanatory deep-dive into what happens from the moment app.Run(args) is called to when a command finishes execution
Reference
- Attribute and Parameter Reference - A summary of all attributes and parameter-related features in Spectre.Console.Cli
- Type Converters - Reference for type conversion in Spectre.Console.Cli command-line parsing
- CommandContext Reference - Reference documentation for the CommandContext class in Spectre.Console.Cli
- Built-in Command Behaviors - A reference describing Spectre.Console.Cli's built-in behaviors and conventions for completeness