Skip to content

Commit 2819f48

Browse files
committed
test that CliConfiguration can be intentionally subclassed
This provides an example of how to use custom types derived from `CliConfiguration` as an extensibility point.
1 parent 299ea1c commit 2819f48

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/System.CommandLine.Tests/CommandLineConfigurationTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,37 @@ public void ThrowIfInvalid_throws_if_a_parentage_cycle_is_detected()
260260
.Should()
261261
.Be($"Cycle detected in command tree. Command '{rootCommand.Name}' is its own ancestor.");
262262
}
263+
264+
[Fact]
265+
public void It_can_be_subclassed_to_provide_additional_context()
266+
{
267+
var command = new CliRootCommand();
268+
var commandWasInvoked = false;
269+
command.SetAction(parseResult =>
270+
{
271+
var appConfig = (CustomAppConfiguration)parseResult.Configuration;
272+
273+
// access custom config
274+
275+
commandWasInvoked = true;
276+
277+
return 0;
278+
});
279+
280+
var config = new CustomAppConfiguration(command);
281+
282+
config.Invoke("");
283+
284+
commandWasInvoked.Should().BeTrue();
285+
}
286+
}
287+
288+
public class CustomAppConfiguration : CliConfiguration
289+
{
290+
public CustomAppConfiguration(CliRootCommand command) : base(command)
291+
{
292+
EnableDefaultExceptionHandler = false;
293+
}
294+
295+
public IServiceProvider ServiceProvider { get; }
263296
}

0 commit comments

Comments
 (0)