diff --git a/Cocona.sln b/Cocona.sln index 111f335..5e100bb 100644 --- a/Cocona.sln +++ b/Cocona.sln @@ -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 @@ -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 @@ -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} diff --git a/samples/MinimalApi.AddCommands/CoconaSample.MinimalApi.AddCommands.csproj b/samples/MinimalApi.AddCommands/CoconaSample.MinimalApi.AddCommands.csproj new file mode 100644 index 0000000..30ff60f --- /dev/null +++ b/samples/MinimalApi.AddCommands/CoconaSample.MinimalApi.AddCommands.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/samples/MinimalApi.AddCommands/Program.cs b/samples/MinimalApi.AddCommands/Program.cs new file mode 100644 index 0000000..f64f73b --- /dev/null +++ b/samples/MinimalApi.AddCommands/Program.cs @@ -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(); diff --git a/src/Cocona.Core/Builder/CoconaCommandsBuilder.cs b/src/Cocona.Core/Builder/CoconaCommandsBuilder.cs index 05cda02..78c5d23 100644 --- a/src/Cocona.Core/Builder/CoconaCommandsBuilder.cs +++ b/src/Cocona.Core/Builder/CoconaCommandsBuilder.cs @@ -160,6 +160,22 @@ public static CommandConventionBuilder AddCommand(this ICoconaCommandsBuilder bu return new CommandConventionBuilder(conventions).FromBuilder().WithMetadata(new PrimaryCommandAttribute()); } + + /// + /// Adds a primary command definition delegate to the builder and return the builder. + /// + /// + /// + /// + public static ICoconaCommandsBuilder AddCommands(this ICoconaCommandsBuilder builder, params (string, Delegate)[] commandBodies) + { + foreach (var (name, commandBody) in commandBodies) + { + builder.AddCommand(name, commandBody); + } + return builder; + } + /// /// Adds a command definition delegate to the builder. /// diff --git a/src/Cocona/CoconaAppExtensions.cs b/src/Cocona/CoconaAppExtensions.cs new file mode 100644 index 0000000..e5f5cf8 --- /dev/null +++ b/src/Cocona/CoconaAppExtensions.cs @@ -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; + } + } +} diff --git a/test/Cocona.Test/Builder/CoconaCommandBuilderTest.cs b/test/Cocona.Test/Builder/CoconaCommandBuilderTest.cs index fc3856a..7fbbb3e 100644 --- a/test/Cocona.Test/Builder/CoconaCommandBuilderTest.cs +++ b/test/Cocona.Test/Builder/CoconaCommandBuilderTest.cs @@ -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(); + built[0].Metadata.Should().HaveCount(2); + built[0].Metadata[0].Should().BeOfType(); + built[1].Should().BeOfType(); + built[1].Metadata.Should().HaveCount(2); + built[1].Metadata[0].Should().BeOfType(); + } + + [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(); + built[0].Metadata.Should().HaveCount(2); + built[0].Metadata[0].Should().BeOfType(); + built[1].Should().BeOfType(); + built[1].Metadata.Should().HaveCount(2); + built[1].Metadata[0].Should().BeOfType(); + } } }