Skip to content

[API Proposal]: Lazy subcommand registration #138204

In review
dotnet/command-line-api
#2901

Description

@adamsitnik

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

  1. added this to the 12.0.0 milestone on Nov 24, 2026
  2. added
    api-ready-for-review
    on Nov 24, 2026
  3. dotnet-policy-service commented on Nov 24, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-console
    See info in area-owners.md if you want to be subscribed.

  4. MihuBot commented on Nov 24, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  5. adamsitnik commented on Nov 24, 2026

    @adamsitnik
    MemberAuthor

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

    It's a duplicate, I've created it here to ensure it gets included in the API review queue.

  6. bartonjs commented on Nov 24, 2026

    @bartonjs
    Member

    Video

    Looks good as proposed.

    The overload with aliases is 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.

    namespace System.CommandLine;
    
    public partial class Command
    {
        public void Add(string name, Func<Command> commandFactory);
        public void Add(
            string name,
            ReadOnlySpan<string> aliases,
            Func<Command> commandFactory);
    }
  7. added
    api-approved
    and removed
    api-ready-for-review
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approvedarea-System.Console

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions