Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 10, 2025

Overview

This PR updates the CommandHandler<TOptions> base class to support System.CommandLine 2.0.0-beta5 API patterns while maintaining complete backward compatibility with existing command handlers.

Problem

System.CommandLine 2.0.0-beta5 introduces breaking changes that affect handler method signatures:

  • Handler methods now use ParseResult instead of individual parameters
  • SetHandler is replaced with SetAction
  • New option creation patterns are required

The existing CommandHandler<TOptions> base class needed to be updated to support these new patterns without breaking existing functionality.

Solution

Key Changes

1. Enhanced ICommandHandler Interface

public interface ICommandHandler<TOptions> where TOptions : ICommandHandlerOptions
{
    // Existing method - unchanged
    Task HandleAsync(TOptions commandOptions);
    
    // New method for beta5 compatibility
    Task HandleAsync(ParseResult parseResult);
}

2. Updated CommandHandler Base Class

  • Added virtual HandleAsync(ParseResult) method that delegates to the existing options-based method
  • Added abstract ExtractCommandOptions(ParseResult) method for derived classes to implement
  • Provided utility methods for ParseResult extraction:
    • GetOptionValue<T>(ParseResult, Option<T>)
    • GetOptionValue<T>(ParseResult, string)
    • HasOption(ParseResult, Option)

3. Migration Pattern
Command handlers implement the extraction method:

protected override MyCommandHandlerOptions ExtractCommandOptions(ParseResult parseResult)
{
    var projectPath = GetOptionValue<DirectoryInfo>(parseResult, "--project");
    var skipTables = GetOptionValue<bool>(parseResult, "--skipTables");
    return new MyCommandHandlerOptions(projectPath, skipTables);
}

Zero Breaking Changes

  • ✅ All existing HandleAsync(TOptions) methods work unchanged
  • ✅ All existing tests pass without modification
  • ✅ All existing SetupCommand methods continue to work
  • ✅ Legacy API remains fully functional until migration

Examples Updated

Updated three command handlers to demonstrate the new pattern:

  • InitProjectCommandHandler
  • ExtractModelCommandHandler
  • QueryModelCommandHandler

Documentation

  • Comprehensive migration guide (docs/COMMANDLINE-BETA5-MIGRATION.md)
  • Practical before/after examples (docs/BETA5-MIGRATION-EXAMPLE.cs)
  • Clear migration timeline and dependencies

Testing

Added test coverage for ParseResult functionality:

[TestMethod]
public async Task HandleAsync_WithParseResult_ShouldInitializeProjectDirectory_WhenProjectPathIsValid()
{
    // Demonstrates ParseResult-based handling works correctly
}

Benefits

  1. Future Ready: Prepared for beta5 API when package is updated
  2. Zero Downtime: Existing code continues working during migration
  3. Type Safety: Strongly-typed options pattern preserved
  4. Consistent: All command handlers follow the same extraction pattern
  5. Testable: Both patterns can be easily unit tested

Next Steps

Fixes #17.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] [Chore]: Update CommandHandler base class for System.CommandLine 2.0.0-beta5 Update CommandHandler base class for System.CommandLine 2.0.0-beta5 compatibility Jul 10, 2025
@Copilot Copilot AI requested a review from ahmedig July 10, 2025 04:05
Copilot finished work on behalf of ahmedig July 10, 2025 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Chore]: Update CommandHandler base class for System.CommandLine 2.0.0-beta5

2 participants