From d827282f9d4439a49ffbff30bc4c4b4789aaaa71 Mon Sep 17 00:00:00 2001 From: Kyle Burrows Date: Thu, 13 Nov 2025 21:50:06 -0500 Subject: [PATCH] Add a dotnet new template --- .github/workflows/cd-build.yml | 8 ++++ README.md | 22 +++++++++- .../MinimalCli.Templates.csproj | 41 +++++++++++++++++++ src/MinimalCli.Templates/README.md | 6 +++ .../console/.template.config/template.json | 13 ++++++ .../content/console/Commands/EchoCommand.cs | 15 +++++++ .../content/console/Commands/GreetCommand.cs | 21 ++++++++++ .../content/console/MinimalCliApp.csproj | 17 ++++++++ .../content/console/Program.cs | 27 ++++++++++++ .../console/Properties/launchSettings.json | 10 +++++ src/MinimalCli.sln | 10 ++++- 11 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 src/MinimalCli.Templates/MinimalCli.Templates.csproj create mode 100644 src/MinimalCli.Templates/README.md create mode 100644 src/MinimalCli.Templates/content/console/.template.config/template.json create mode 100644 src/MinimalCli.Templates/content/console/Commands/EchoCommand.cs create mode 100644 src/MinimalCli.Templates/content/console/Commands/GreetCommand.cs create mode 100644 src/MinimalCli.Templates/content/console/MinimalCliApp.csproj create mode 100644 src/MinimalCli.Templates/content/console/Program.cs create mode 100644 src/MinimalCli.Templates/content/console/Properties/launchSettings.json diff --git a/.github/workflows/cd-build.yml b/.github/workflows/cd-build.yml index 00d3bff..540fe39 100644 --- a/.github/workflows/cd-build.yml +++ b/.github/workflows/cd-build.yml @@ -26,8 +26,12 @@ jobs: # packs all nuget packages at once - name: Pack Minimal Command Line Nuget Alpha run: dotnet pack src/MinimalCli/MinimalCli.csproj -p:PackageVersion=2.0.0.${{github.run_number}}-alpha -o ./packages + - name: Pack Minimal Command Line Template Nuget Alpha + run: dotnet pack src/MinimalCli.Templates/MinimalCli.Templates.csproj -p:PackageVersion=2.0.0.${{github.run_number}}-alpha -o ./packages - name: Pack Minimal Command Line Nuget run: dotnet pack src/MinimalCli/MinimalCli.csproj -p:PackageVersion=2.0.0.${{github.run_number}} -o ./packages + - name: Pack Minimal Command Line Template Nuget + run: dotnet pack src/MinimalCli.Templates/MinimalCli.Templates.csproj -p:PackageVersion=2.0.0.${{github.run_number}} -o ./packages - name: Publish Minimal Command Line Artifacts uses: actions/upload-artifact@v4 @@ -52,6 +56,8 @@ jobs: run: ls -R - name: Push Minimal Command Line Nuget run: dotnet nuget push ./MinimalCli.2.0.0.${{github.run_number}}-alpha.nupkg --api-key ${{secrets.NUGETKEY}} --source https://api.nuget.org/v3/index.json + - name: Push Minimal Command Line Template Nuget + run: dotnet nuget push ./MinimalCli.Templates.2.0.0.${{github.run_number}}-alpha.nupkg --api-key ${{secrets.NUGETKEY}} --source https://api.nuget.org/v3/index.json production: needs: integration @@ -67,3 +73,5 @@ jobs: - name: Push Minimal Command Line Nuget run: dotnet nuget push ./MinimalCli.2.0.0.${{github.run_number}}.nupkg --api-key ${{secrets.NUGETKEY}} --source https://api.nuget.org/v3/index.json + - name: Push Minimal Command Line Template Nuget + run: dotnet nuget push ./MinimalCli.Templates.2.0.0.${{github.run_number}}.nupkg --api-key ${{secrets.NUGETKEY}} --source https://api.nuget.org/v3/index.json diff --git a/README.md b/README.md index ef6187f..9f8c33f 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ > System.CommandLine and to create commands with minimal boilerplate. All you have to do > to turn a function into a command is decorate it with the `[Handler]` attribute! -## Getting Started +# Getting Started ### Hello World: @@ -48,7 +48,23 @@ await app.StartAsync(); This will map the HelloWorld class's `void Execute(..)` function to a command called `"hello"`, and it will map the parameter `message` to a string Argument called ``. -## Installing MinimalCommandLine +# Installation + +## Easy: using the project template + +Simply run the following using the dotnet cli: + +``` +dotnet new install MinimalCli.Templates +``` + +Then you should be able to create a new MinimalCli project like this: + +``` +dotnet new minCli -n MyMinimalCliApp +``` + +## Advanced: Manually installing MinimalCli NuGet package Add a reference to the nuget package `MinimalCommandLine`. @@ -63,6 +79,8 @@ Add a reference to the nuget package `MinimalCommandLine`. * Select the project you want to install it into * Hit Install +# Examples + ## Simple Examples: ### Simple Command - Defaults: diff --git a/src/MinimalCli.Templates/MinimalCli.Templates.csproj b/src/MinimalCli.Templates/MinimalCli.Templates.csproj new file mode 100644 index 0000000..cb005bb --- /dev/null +++ b/src/MinimalCli.Templates/MinimalCli.Templates.csproj @@ -0,0 +1,41 @@ + + + + MinimalCli.Templates + Minimal Command Line Application Template + dotnetKyle + A working project starting point for a MinimalCli console application. + Command; Line; System.CommandLine; Minimal; Source Generator; Minimal CLI; CLI + https://github.com/dotnetKyle/MinimalCommandLine + https://github.com/dotnetKyle/MinimalCommandLine + README.md + + + Template + netstandard2.0 + true + false + content + + $(NoWarn);NU5128 + true + + + + false + + + + + + + + + + + + + + + + diff --git a/src/MinimalCli.Templates/README.md b/src/MinimalCli.Templates/README.md new file mode 100644 index 0000000..ff257b9 --- /dev/null +++ b/src/MinimalCli.Templates/README.md @@ -0,0 +1,6 @@ + +# Minimal CLI Project Template + +A "dotnet new" project template to create a MinimalCli console app that uses the Minimal CLI source generator + +Use `dotnet new install MinimalCli.Templates` to install this template and enable you to run `dotnet new minCli` to create ready to go new Minimal CLI projects. \ No newline at end of file diff --git a/src/MinimalCli.Templates/content/console/.template.config/template.json b/src/MinimalCli.Templates/content/console/.template.config/template.json new file mode 100644 index 0000000..9470b5f --- /dev/null +++ b/src/MinimalCli.Templates/content/console/.template.config/template.json @@ -0,0 +1,13 @@ +{ + "$schema": "http://json.schemastore.org/template", + "author": "dotnetKyle", + "classifications": [ "Common", "Console" ], + "identity": "ExampleTemplate.AsyncProject", + "name": "MinimalCli Console App", + "shortName": "minCli", + "sourceName":"MinimalCliApp", + "tags": { + "language": "C#", + "type": "project" + } +} \ No newline at end of file diff --git a/src/MinimalCli.Templates/content/console/Commands/EchoCommand.cs b/src/MinimalCli.Templates/content/console/Commands/EchoCommand.cs new file mode 100644 index 0000000..3c1f0ba --- /dev/null +++ b/src/MinimalCli.Templates/content/console/Commands/EchoCommand.cs @@ -0,0 +1,15 @@ +using MinimalCli; + +namespace MinimalCliApp.Commands; + +internal class EchoCommand +{ + // creates a sub-command that can be called by using the 'echo-command' sub-command + [Handler("echo-command")] + public Task ExecuteAsync(string prompt) + { + Console.WriteLine("Echo: {0}", prompt); + + return Task.CompletedTask; + } +} diff --git a/src/MinimalCli.Templates/content/console/Commands/GreetCommand.cs b/src/MinimalCli.Templates/content/console/Commands/GreetCommand.cs new file mode 100644 index 0000000..d1196cd --- /dev/null +++ b/src/MinimalCli.Templates/content/console/Commands/GreetCommand.cs @@ -0,0 +1,21 @@ +using MinimalCli; + +namespace MinimalCliApp.Commands; + +internal class GreetCommand +{ + // this attribute creates the main command handler + [RootHandler] + public void Execute(string? yourName = null) + { + if(!string.IsNullOrEmpty(yourName)) + { + Console.WriteLine("Hello {0}!", yourName); + return; + } + else + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/src/MinimalCli.Templates/content/console/MinimalCliApp.csproj b/src/MinimalCli.Templates/content/console/MinimalCliApp.csproj new file mode 100644 index 0000000..6f33e3e --- /dev/null +++ b/src/MinimalCli.Templates/content/console/MinimalCliApp.csproj @@ -0,0 +1,17 @@ + + + + Exe + net8.0 + enable + enable + + true + MinimalCliApp + + + + + + + diff --git a/src/MinimalCli.Templates/content/console/Program.cs b/src/MinimalCli.Templates/content/console/Program.cs new file mode 100644 index 0000000..d6c079f --- /dev/null +++ b/src/MinimalCli.Templates/content/console/Program.cs @@ -0,0 +1,27 @@ +// See https://github.com/dotnetKyle/MinimalCommandLine for more information +using MinimalCli; + +var builder = new MinimalCommandLineBuilder(args); + +// configure the root command's options +builder.MapRootCommand(options => { + options.Command.Description = "A greeter command"; + + options.YourNameOption.Description = "Your name so the command can greet you."; +}); + +// modify the echo command's options +builder.MapEchoCommandCommand(options => +{ + options.Command.Description = "A command that echoes a prompt back to the console"; + + options.PromptArgument.Description = "The prompt to echo back"; + options.PromptArgument.DefaultValueFactory = _ => "echo"; +}); + + +builder.MapAllCommands(); + +var app = builder.Build(); + +await app.StartAsync(); diff --git a/src/MinimalCli.Templates/content/console/Properties/launchSettings.json b/src/MinimalCli.Templates/content/console/Properties/launchSettings.json new file mode 100644 index 0000000..267ef69 --- /dev/null +++ b/src/MinimalCli.Templates/content/console/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "MinimalCliApp": { + "commandName": "Project", + // commandLineArgs will get called on the console application when you use Start (F5) + "commandLineArgs": "-h" + //"commandLineArgs": "echo-command \"foo\"" + } + } +} \ No newline at end of file diff --git a/src/MinimalCli.sln b/src/MinimalCli.sln index 279cdd5..a99bcc3 100644 --- a/src/MinimalCli.sln +++ b/src/MinimalCli.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.3.32804.467 +# Visual Studio Version 18 +VisualStudioVersion = 18.0.11205.157 d18.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DemoApp", "DemoApp\DemoApp.csproj", "{C10C4313-AF8A-437C-825F-A7733F288DEE}" EndProject @@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalCli.Core", "MinimalC EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalCli.SourceGenerator", "MinimalCli.SourceGenerator\MinimalCli.SourceGenerator.csproj", "{5AF84270-8ECB-4097-A8DA-9C676E1E9373}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalCli.Templates", "MinimalCli.Templates\MinimalCli.Templates.csproj", "{01AD5287-A48C-880F-E2F6-5FD7E4EDA76B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -46,6 +48,10 @@ Global {5AF84270-8ECB-4097-A8DA-9C676E1E9373}.Debug|Any CPU.Build.0 = Debug|Any CPU {5AF84270-8ECB-4097-A8DA-9C676E1E9373}.Release|Any CPU.ActiveCfg = Release|Any CPU {5AF84270-8ECB-4097-A8DA-9C676E1E9373}.Release|Any CPU.Build.0 = Release|Any CPU + {01AD5287-A48C-880F-E2F6-5FD7E4EDA76B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {01AD5287-A48C-880F-E2F6-5FD7E4EDA76B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {01AD5287-A48C-880F-E2F6-5FD7E4EDA76B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {01AD5287-A48C-880F-E2F6-5FD7E4EDA76B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE