From 1a419cc3b71b6c7011ec14e1ad5d7488c4e7a948 Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Tue, 4 Apr 2023 15:04:11 -0700 Subject: [PATCH] test that CliConfiguration can be intentionally subclassed This provides an example of how to use custom types derived from `CliConfiguration` as an extensibility point. --- .../CommandLineConfigurationTests.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/System.CommandLine.Tests/CommandLineConfigurationTests.cs b/src/System.CommandLine.Tests/CommandLineConfigurationTests.cs index d6fae8df50..b725eeba1a 100644 --- a/src/System.CommandLine.Tests/CommandLineConfigurationTests.cs +++ b/src/System.CommandLine.Tests/CommandLineConfigurationTests.cs @@ -260,4 +260,37 @@ public void ThrowIfInvalid_throws_if_a_parentage_cycle_is_detected() .Should() .Be($"Cycle detected in command tree. Command '{rootCommand.Name}' is its own ancestor."); } + + [Fact] + public void It_can_be_subclassed_to_provide_additional_context() + { + var command = new CliRootCommand(); + var commandWasInvoked = false; + command.SetAction(parseResult => + { + var appConfig = (CustomAppConfiguration)parseResult.Configuration; + + // access custom config + + commandWasInvoked = true; + + return 0; + }); + + var config = new CustomAppConfiguration(command); + + config.Invoke(""); + + commandWasInvoked.Should().BeTrue(); + } +} + +public class CustomAppConfiguration : CliConfiguration +{ + public CustomAppConfiguration(CliRootCommand command) : base(command) + { + EnableDefaultExceptionHandler = false; + } + + public IServiceProvider ServiceProvider { get; } } \ No newline at end of file