Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Spectre.Console.Cli/ConfiguratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,16 @@ public static ICommandConfigurator AddAsyncDelegate(
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
public static ICommandConfigurator AddDelegate<TSettings>(
this IConfigurator<TSettings> configurator,
this IConfigurator<TSettings>? configurator,
string name,
Func<CommandContext, int> func)
where TSettings : CommandSettings
where TSettings : CommandSettings
{
if (typeof(TSettings).IsAbstract)
{
AddDelegate(configurator as IConfigurator<EmptyCommandSettings>, name, func);
}

if (configurator == null)
{
throw new ArgumentNullException(nameof(configurator));
Expand Down
49 changes: 49 additions & 0 deletions test/Spectre.Console.Cli.Tests/Unit/CommandAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,55 @@ public void Should_Execute_Delegate_Command_At_Root_Level()
data.ShouldBe(2);
}

[Fact]
public void Should_Execute_Nested_Delegate_Empty_Command()
{
// Given
var app = new CommandAppTester();
app.Configure(cfg =>
{
cfg.AddBranch("a", d =>
{
d.AddDelegate("b", _ =>
{
AnsiConsole.MarkupLine("[red]Complete[/]");
return 0;
});
});
});

// When
var result = app.Run([
"a", "b"
]);

// Then
result.ExitCode.ShouldBe(0);
}

[Fact]
public void Should_Execute_Delegate_Empty_Command_At_Root_Level()
{
// Given
var app = new CommandAppTester();
app.Configure(cfg =>
{
cfg.AddDelegate("a", _ =>
{
AnsiConsole.MarkupLine("[red]Complete[/]");
return 0;
});
});

// When
var result = app.Run([
"a"
]);

// Then
result.ExitCode.ShouldBe(0);
}

[Fact]
public async void Should_Execute_Async_Delegate_Command_At_Root_Level()
{
Expand Down