CLI framework for C# (.NET): System.CommandLine and Spectre.Console

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

CLI framework for C# (.NET): System.CommandLine and Spectre.Console



Creating high-quality console applications (CLI) in C# is a task that requires not only command-line argument parsing, but also convenient data presentation, subcommand support, validation, and interactivity. In the .NET ecosystem, there are two powerful tools for this: the official System.CommandLine from Microsoft and the versatile library Spectre.Console. In this article, we will thoroughly examine their capabilities, show usage examples, and compare approaches.



1. System.CommandLine: official parser from Microsoft



System.CommandLine is an open-source library that provides a declarative way to define commands, arguments, options, and handlers. It is part of the .NET SDK (starting from .NET 6) and is available as a NuGet package.



Key features

  • Declarative description of command structure using attributes or fluent API.
  • Automatic help generation (--help).
  • Support for nested commands (subcommands) and aliases.
  • Validation of arguments and options (types, ranges, required).
  • Integration with DI containers and middleware.


Example: simple application with two commands

Let's create a console application that supports greet and calc commands.



using System.CommandLine;

var rootCommand = new RootCommand("Multi-functional CLI application");

// greet commandvar greetCommand = new Command("greet", "Greet the user");var nameOption = new Option<string>( "--name", () => "world", "Name for greeting");greetCommand.AddOption(nameOption);

greetCommand.SetHandler((string name) =>{ Console.WriteLine($"Hello, {name}!");}, nameOption);

// calc commandvar calcCommand = new Command("calc", "Simple calculator");var aArgument = new Argument<int>("a", "First number");var bArgument = new Argument<int>("b", "Second number");var operationOption = new Option<string>( "--operation", () => "add", "Operation: add, sub, mul, div");calcCommand.AddArgument(aArgument);calcCommand.AddArgument(bArgument);calcCommand.AddOption(operationOption);

calcCommand.SetHandler((int a, int b, string op) =>{ var result = op switch { "add" => a + b, "sub" => a - b, "mul" => a * b, "div" => b != 0 ? a / b : throw new DivideByZeroException(), _ => throw new ArgumentException("Unknown operation") }; Console.WriteLine($"Result: {result}");}, aArgument, bArgument, operationOption);

rootCommand.AddCommand(greetCommand);rootCommand.AddCommand(calcCommand);

return await rootCommand.InvokeAsync(args);


Running the application:

dotnet run -- greet --name "PythonLib"dotnet run -- calc 10 5 --operation mul


2. Spectre.Console: beauty and interactivity



Spectre.Console is a library for creating beautiful console interfaces. It does not replace an argument parser, but perfectly complements it: colored output, tables, progress bars, interactive menus, and even drawing in the console.



Key features

  • Colored and formatted output with Markdown support.
  • Tables, charts, calendars.
  • Interactive components: list selection, text input, confirmation.
  • Animation (spinners, progress bars).
  • Drawing borders, panels, and columns.


Example: interactive menu and table

Let's show how to create a selection menu and display data in a table format.



using Spectre.Console;

// Interactive menuvar choice = AnsiConsole.Prompt( new SelectionProm

Recommendations