dotnet-system-commandline

Implement CLI argument parsing for .NET apps using System.CommandLine 2.0.x GA idioms.

2|Updated Jul 18, 2026
One-click install
npx skills add https://github.com/Arasz/ai-badger --skill dotnet-system-commandline-arasz
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-system-commandline
Source: https://github.com/Arasz/ai-badger/tree/main/features/dotnet/skills/dotnet-workload/references/dotnet-system-commandline
Command: npx skills add https://github.com/Arasz/ai-badger --skill dotnet-system-commandline-arasz

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Adding CLI argument parsing to .NET console apps and global dotnet tools is full of traps: System.CommandLine's GA API differs sharply from the beta API shown in most blog posts, help output defaults to stdout which corrupts stdio MCP protocol streams, and archived parsers like Cocona still lurk in existing tools. This Skill provides empirically verified, version-pinned guidance so you avoid compile errors, silent misbehavior, and protocol corruption. ## Core Features & Use Cases - Parser landscape verdicts: Compares System.CommandLine 2.0.x GA, Spectre.Console.Cli, Cocona (archived — do not adopt), and McMaster.Extensions, with maintenance guidance for existing Cocona-based tools. - Pinned GA API idioms: Parse-first patterns, HelpAction/VersionOptionAction detection, Option.Validators for custom value grammars, instance-based GetValue reads for duplicate aliases, and the two-root pattern for optional subcommands. - Stdio MCP / JSON-RPC safety: Ensures help, version, and error text render to stderr so they never corrupt the protocol stream on stdout, with test patterns to lock the behavior. - Use Case: You are building a dotnet global tool that exposes an MCP server over stdio. Use this Skill to parse args with System.CommandLine 2.0.10, route all CLI text to stderr, handle the WebApplicationFactory hidden-flag triplet in E2E tests, and keep secrets out of argv. ## Quick Start Ask the agent to add System.CommandLine 2.0.10 argument parsing to your .NET tool with help and version routed to stderr for stdio MCP compatibility.

Frequently Asked Questions about dotnet-system-commandline

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I parse command-line arguments in .NET with System.CommandLine 2.0 GA?

Call root.Parse(args, new ParserConfiguration { EnablePosixBundling = true }) directly — the GA release removed CommandLineBuilder and UseDefaults. Inspect parseResult.Action for HelpAction, check parseResult.Errors, then render via parseResult.Invoke with an InvocationConfiguration.

System.CommandLine vs Spectre.Console.Cli vs Cocona — which should I use?

System.CommandLine 2.0.x GA is the default choice for flat option sets: it is MIT-licensed, dependency-free on net8.0, and used by the dotnet CLI itself. Spectre.Console.Cli suits nested command frameworks with DI. Cocona is archived since 2024 — do not adopt it for new work.

Why does System.CommandLine help output break my stdio MCP server?

System.CommandLine renders help to stdout by default, which corrupts the JSON-RPC protocol stream on stdout. Fix it by passing InvocationConfiguration with both Output and Error set to Console.Error so all CLI text goes to stderr, and pin it with a test asserting zero bytes on stdout.

Does System.CommandLine 2.0.10 support GetValueForOption or CommandLineBuilder?

No — both are pre-GA beta APIs and fail to compile against 2.0.10 (CS1061). The GA equivalents are ParseResult.GetValue<T>(Option<T>) for instance-based reads and direct Command.Parse calls instead of CommandLineBuilder.UseDefaults().

Why do my WebApplicationFactory E2E tests fail after switching to parse-first args?

WebApplicationFactory invokes your real entry point with --environment, --contentRoot, and --applicationName, which a strict parser rejects as unknown options. Declare these three as hidden no-op options on the root command so the triplet parses with zero errors.

How do I validate custom option values like '4h' durations in System.CommandLine GA?

Use Option.Validators, the only native parse-error hook in GA — there is no ParseArgument. Add a validator delegate that reads the raw token via GetValueOrDefault<string>() and calls result.AddError on failure; keep the option as Option<string> with a pure static TryParse.