CLI frameworks for C# (.NET): creating professional console applications
Modern C# (.NET) development is not limited to web applications and desktop programs. Console applications (CLI) remain in demand for automation, DevOps tools, administration utilities, and internal scripts. However, manual command-line argument parsing is a tedious and error-prone task. Specialized CLI frameworks come to the rescue, handling argument parsing, help generation, validation, and even colored output.
In this article, we will look at three of the most popular open-source solutions for creating CLI in C# (.NET): System.CommandLine (official package from Microsoft), Spectre.Console, and Cocona. You will learn their key features, see code examples, and be able to choose the right tool for your project.
1. System.CommandLine (Microsoft)
System.CommandLine is an official library from Microsoft designed for creating modern console applications with rich support for argument parsing, validation, autocompletion, and help generation. It is actively used in the .NET SDK and other Microsoft tools.
Key features:
- Declarative definition of commands, options, and arguments.
- Automatic help generation (--help).
- Middleware support for error handling and logging.
- Built-in type and value validation.
- Integration with Dependency Injection.
Code example:
using System.CommandLine;
var rootCommand = new RootCommand("Simple CLI calculator");
var argA = new Argument<int>("a", "First number");var argB = new Argument<int>("b", "Second number");var optMultiply = new Option<bool>("--multiply", "Multiply instead of adding");
rootCommand.AddArgument(argA);rootCommand.AddArgument(argB);rootCommand.AddOption(optMultiply);
rootCommand.SetHandler((int a, int b, bool multiply) =>{ int result = multiply ? a * b : a + b; Console.WriteLine($"Result: {result}");}, argA, argB, optMultiply);
await rootCommand.InvokeAsync(args);Run: dotnet run -- 5 3 --multiply will output Result: 15.
2. Spectre.Console
Spectre.Console is not just a CLI framework, but a whole ecosystem for creating beautiful and interactive console applications. It includes argument parsing (via the Spectre.Console.Cli package), colored output, tables, progress bars, calendars, and even animation.
Key features:
- Declarative command definition with attributes.
- Automatic help generation with color styling.
- Built-in DI and validation support.
- Rich set of widgets: tables, trees, panels, calendars.
- Interactive prompts (list selection, confirmation, text input).
Code example (command with attributes):
using Spectre.Console;using Spectre.Console.Cli;
public class GreetCommand : Command<GreetSettings>{ public override int Execute(CommandContext context, GreetSettings settings) { AnsiConsole.MarkupLine($"[bold green]Hello, {settings.Name}![/]"); if (settings.Repeat > 1) { AnsiConsole.MarkupLine($"[yellow]Repeated {settings.Repeat} time(s)[/]"); } return 0; }}
public class GreetSettings : CommandSettings{ [CommandArgument(0, "[Name]")] public string Name { get; set; } = "World";
[CommandOption("-r|--repeat <N>")] public int Repeat { get; set; } = 1;}
// In Program.csvar app = new CommandApp<GreetCommand>();app.Run(args);Re