Repository navigation
Description
Background and motivation
System.CommandLine.Command currently requires every subcommand subtree to be instantiated up front and added to Command.Subcommands before Command.Parse(...) runs. Constructing a Command in a large CLI typically allocates all of its Option<T> and Argument<T> symbols, nested subcommands, validators, action/completion delegates, and localized .resx Description / HelpName strings—even though CommandLineParser.Tokenize only reads direct child Name and Aliases strings when building ValidTokens(), and ParseOperation never visits unmatched sibling subtrees during parsing, validation, or subcommand --help.
API Proposal
namespace System.CommandLine;
public class Command
{
public IList<Command> Subcommands { get; } // Existing signature, unchanged.
public void Add(Command command) => Subcommands.Add(command); // Existing signature, unchanged.
public void Add(string name, Func<Command> commandFactory);
public void Add(
string name,
ReadOnlySpan<string> aliases,
Func<Command> commandFactory);
}API Usage
using System.CommandLine;
RootCommand root = new("dotnet");
root.Add("build", static () => CreateBuildCommand());
root.Add("solution", ["sln"], static () =>
{
Command solution = new("solution", "Modify solution files");
solution.Add("list", static () => CreateSolutionListCommand());
solution.Add("add", static () => CreateSolutionAddCommand());
return solution;
});
// Tokenizes "sln" -> materializes only "solution", then "list" -> materializes only "list".
// "build" and "solution add" are never allocated.
ParseResult result = root.Parse(["sln", "list"]);
return result.Invoke();Alternative Designs
Command.Subcommands could return a new public CommandList type (still implements IList<Command>) that allows registering subcommands lazily:
public void Add(string name, Func<Command> factory);
public void Add(string name, string[] aliases, Func<Command> factory);This would be a breaking change.
Risks
Creating invalid command could throw lazily, depending on the input (currently entire symbol tree has the be created up front)
Activity
dotnet-policy-service commentedon Nov 24, 2026
Tagging subscribers to this area: @dotnet/area-system-console
See info in area-owners.md if you want to be subscribed.
MihuBot commentedon Nov 24, 2026
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):
adamsitnik commentedon Nov 24, 2026
It's a duplicate, I've created it here to ensure it gets included in the API review queue.
bartonjs commentedon Nov 24, 2026
Video
Looks good as proposed.
The overload with
aliasesis being added in the middle, but we feel that's aesthetically OK so that the factory-lambda is the last argument, and there's no positional confusion as they're distinct types.