Add options validation support to cli hosting and some minor changes for running the CliHost#65
Conversation
- Introduce `UseValidation` in `ICliHostBuilder` and `CliHostSettings` to enable command options validation. - Add `IOptionsValidation` interface and `OptionsValidationResult` for asynchronous options validation. - Enhance `DefaultCliHost` with validation logic and custom `CliCommandOptionsInvalidException`. - Extend `CliExitCodes` with `CommandOptionsInvalid`. - Add and update unit tests to cover validation scenarios.
There was a problem hiding this comment.
Pull request overview
This PR adds support for command options validation in the CLI hosting framework and introduces a new RunMainAsync method for direct integer exit code return. The implementation allows commands to implement IOptionsValidation interface for custom validation logic with automatic validation execution when enabled.
Key changes:
- Added options validation support via
IOptionsValidationinterface andOptionsValidationResultclass - Introduced
UseValidation()method onICliHostBuilderto enable/disable validation - Added
RunMainAsync()convenience method toICliHostfor returning exit codes directly as integers
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
source/Cli/CreativeCoders.Cli.Core/IOptionsValidation.cs |
New interface for command options validation |
source/Cli/CreativeCoders.Cli.Core/OptionsValidationResult.cs |
New class to represent validation results with status and messages |
source/Cli/CreativeCoders.Cli.Core/CommandResult.cs |
Added constructors for convenient exit code initialization |
source/Cli/CreativeCoders.Cli.Hosting/ICliHost.cs |
Added RunMainAsync method for direct integer exit code return |
source/Cli/CreativeCoders.Cli.Hosting/ICliHostBuilder.cs |
Added UseValidation method to enable/disable options validation |
source/Cli/CreativeCoders.Cli.Hosting/DefaultCliHost.cs |
Implemented validation logic, RunMainAsync method, and added CliHostSettings class |
source/Cli/CreativeCoders.Cli.Hosting/DefaultCliHostBuilder.cs |
Implemented UseValidation method to configure validation settings |
source/Cli/CreativeCoders.Cli.Hosting/Exceptions/CliCommandOptionsInvalidException.cs |
New exception type for invalid command options |
source/Cli/CreativeCoders.Cli.Hosting/CliExitCodes.cs |
Added CommandOptionsInvalid exit code constant |
tests/CreativeCoders.Cli.Tests/Hosting/DefaultCliHostTests.cs |
Added comprehensive tests for validation scenarios and RunMainAsync |
tests/CreativeCoders.Cli.Tests/Hosting/DefaultCliHostBuilderTests.cs |
Added tests for UseValidation configuration |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| settings?.UseValidation | ||
| .Should().NotBe(true); |
There was a problem hiding this comment.
The assertion "Should().NotBe(true)" is an indirect way of checking that validation is disabled. This could pass if settings is null or if UseValidation is false. Consider being more explicit: check if settings is null OR if it exists, verify that UseValidation is explicitly false. This would make the test intent clearer and distinguish between "settings not added" versus "settings added with validation disabled".
| settings?.UseValidation | |
| .Should().NotBe(true); | |
| if (settings == null) | |
| { | |
| settings.Should().BeNull(); | |
| } | |
| else | |
| { | |
| settings.UseValidation.Should().BeFalse(); | |
| } |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ionsInvalidException.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
No description provided.