Update CommandHandler base class for System.CommandLine 2.0.0-beta5 compatibility #26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
ParseResult
instead of individual parametersSetHandler
is replaced withSetAction
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
2. Updated CommandHandler Base Class
HandleAsync(ParseResult)
method that delegates to the existing options-based methodExtractCommandOptions(ParseResult)
method for derived classes to implementGetOptionValue<T>(ParseResult, Option<T>)
GetOptionValue<T>(ParseResult, string)
HasOption(ParseResult, Option)
3. Migration Pattern
Command handlers implement the extraction method:
Zero Breaking Changes
HandleAsync(TOptions)
methods work unchangedSetupCommand
methods continue to workExamples Updated
Updated three command handlers to demonstrate the new pattern:
InitProjectCommandHandler
ExtractModelCommandHandler
QueryModelCommandHandler
Documentation
docs/COMMANDLINE-BETA5-MIGRATION.md
)docs/BETA5-MIGRATION-EXAMPLE.cs
)Testing
Added test coverage for ParseResult functionality:
Benefits
Next Steps
ExtractCommandOptions
in follow-up PRsSetupCommand
methods will migrate toSetAction
after issues [Chore]: Update System.CommandLine package reference to 2.0.0-beta5 #15-16 are completedFixes #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.