Skip to content
This repository was archived by the owner on Dec 14, 2025. It is now read-only.
Draft
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
7 changes: 7 additions & 0 deletions Cocona.sln
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution", "Solution", "{72
Directory.Build.props = Directory.Build.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoconaSample.MinimalApi.AddCommands", "samples\MinimalApi.AddCommands\CoconaSample.MinimalApi.AddCommands.csproj", "{48B3DD2C-AF52-4E9D-8F1E-72266B557C40}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -244,6 +246,10 @@ Global
{7A178909-623C-4788-ADD4-9437091128BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A178909-623C-4788-ADD4-9437091128BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A178909-623C-4788-ADD4-9437091128BA}.Release|Any CPU.Build.0 = Release|Any CPU
{48B3DD2C-AF52-4E9D-8F1E-72266B557C40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48B3DD2C-AF52-4E9D-8F1E-72266B557C40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48B3DD2C-AF52-4E9D-8F1E-72266B557C40}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48B3DD2C-AF52-4E9D-8F1E-72266B557C40}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -283,6 +289,7 @@ Global
{51E42619-D33A-4046-A80D-A979591C19D3} = {26F0BA96-C75C-4BDF-A1CC-3A9F58A16D9E}
{7A178909-623C-4788-ADD4-9437091128BA} = {26F0BA96-C75C-4BDF-A1CC-3A9F58A16D9E}
{72AC9837-3045-4692-909B-1BD04F408B48} = {09F6C99E-D874-4C82-90AA-A37E5BE707C4}
{48B3DD2C-AF52-4E9D-8F1E-72266B557C40} = {26F0BA96-C75C-4BDF-A1CC-3A9F58A16D9E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8DBA26EF-235B-4656-A5DD-00C266A42735}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Cocona\Cocona.csproj" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions samples/MinimalApi.AddCommands/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Cocona;
CoconaApp
.CreateBuilder()
.Build()
.AddCommand("hello", (string name) => Console.WriteLine($"Hello {name}!"))
.AddCommand("bye", (string name) => Console.WriteLine($"Bye {name}!"))
.Run();
16 changes: 16 additions & 0 deletions src/Cocona.Core/Builder/CoconaCommandsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ public static CommandConventionBuilder AddCommand(this ICoconaCommandsBuilder bu
return new CommandConventionBuilder(conventions).FromBuilder().WithMetadata(new PrimaryCommandAttribute());
}


/// <summary>
/// Adds a primary command definition delegate to the builder and return the builder.
/// </summary>
/// <param name="builder"></param>
/// <param name="commandBody"></param>
/// <returns></returns>
public static ICoconaCommandsBuilder AddCommands(this ICoconaCommandsBuilder builder, params (string, Delegate)[] commandBodies)
{
foreach (var (name, commandBody) in commandBodies)
{
builder.AddCommand(name, commandBody);
}
return builder;
}

/// <summary>
/// Adds a command definition delegate to the builder.
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions src/Cocona/CoconaAppExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Cocona.Builder;

namespace Cocona
{
public static class CoconaAppExtensions
{
public static CoconaApp AsCoconaApp(this ICoconaCommandsBuilder coconaCommandsBuilder) =>
(CoconaApp)coconaCommandsBuilder;

public static CoconaApp AddCommand(this CoconaApp app, string name, Delegate @delegate)
{
_ = CommandsBuilderExtensions.AddCommand(app, name, @delegate);
return app;
}
}
}
35 changes: 35 additions & 0 deletions test/Cocona.Test/Builder/CoconaCommandBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,40 @@ class UseFilter_Filter2 : IFilterFactory
}
class UseFilter_Filter3 : IFilterMetadata
{ }

[Fact]
public void AddCommands_Chained()
{
var builder = new CoconaCommandsBuilder();
builder.AddCommands(("1", () => { })).AddCommands(("2", () => { }));

var built = builder.Build();
built.Should().HaveCount(2);
built[0].Should().BeOfType<DelegateCommandData>();
built[0].Metadata.Should().HaveCount(2);
built[0].Metadata[0].Should().BeOfType<CommandFromBuilderMetadata>();
built[1].Should().BeOfType<DelegateCommandData>();
built[1].Metadata.Should().HaveCount(2);
built[1].Metadata[0].Should().BeOfType<CommandFromBuilderMetadata>();
}

[Fact]
public void AddCommands_Multiparams()
{
var builder = new CoconaCommandsBuilder();
builder.AddCommands(
("1", () => { }),
("2", () => { })
);

var built = builder.Build();
built.Should().HaveCount(2);
built[0].Should().BeOfType<DelegateCommandData>();
built[0].Metadata.Should().HaveCount(2);
built[0].Metadata[0].Should().BeOfType<CommandFromBuilderMetadata>();
built[1].Should().BeOfType<DelegateCommandData>();
built[1].Metadata.Should().HaveCount(2);
built[1].Metadata[0].Should().BeOfType<CommandFromBuilderMetadata>();
}
}
}