Skip to content

About

Templates

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

TemplatesHD

Project templates for .NET and for GitHub projects.

Documentation site: Templates Home

Template Location What it is
ConsoleTemplate Src-DotNet-ConsoleDiCmdLine Console app targeting .NET 10 with dependency injection, typed command-line options and a configurable run lifecycle. Installable with dotnet new.
WinForm with HTTP server Src-DotNet-WinFormWithApi A setup guide rather than code: adding a self-hosted HTTP API to a WinForms app. Targets .NET 10; the code in it is verified to compile and to serve while the window is open.
GitHub project starter Src-GitHubProjectStarter PowerShell scaffolding for a new project's docs site, GitHub templates and root files. Parameterised, with -WhatIf support.

Using the ConsoleTemplate template

Console application template with dependency injection, command-line parameters and a configurable run lifecycle.

Full reference — lifecycle, exit codes, dependency injection, logging, extending it: Src-DotNet-ConsoleDiCmdLine/README.md.

Requirements

  • .NET SDK 10.0 or later:

    dotnet --version

1. Install the template

dotnet new install takes a folder that contains a .template.config/template.json, so clone the repository and install from the template folder:

git clone https://github.com/Hyper-Dragon/TemplatesHD.git
cd TemplatesHD
dotnet new install ./Src-DotNet-ConsoleDiCmdLine

Confirm it registered:

dotnet new list console-template
Template Name                                                      Short Name        Language  Tags
-----------------------------------------------------------------  ----------------  --------  -----------
Console app with dependency injection and command-line parameters  console-template  [C#]      Console/CLI

2. Generate a project

dotnet new console-template --name MyTool -o MyTool
cd MyTool

--name replaces ConsoleTemplate everywhere: file names, the project and assembly name, and every namespace. Use -o to choose the output folder independently of the project name.

3. Run it

Check the toolchain first, because this prints and exits immediately:

dotnet run -- --version

Then the full lifecycle, including the banner, the parameter summary and the timed section:

dotnet run -- -s

-s (--silent) skips the confirmation prompt. Without it you are asked <Continue? [Y/N/Q]>; N, Q and Esc all decline. When the output is redirected or there is no terminal attached — a pipeline, a CI job, a container — nothing prompts and nothing pauses, so the same binary is safe to script.

4. Fill it in

Only ConsoleApplication.cs has to change:

Method Purpose
PreRunImplAsync Setup. Return false to skip the main body.
RunImplAsync The main body. Return the exit code, 0 for success.
PostRunImplAsync Teardown or reporting. Return false to report failure.

Add a command-line option by declaring an attributed property on CommandLineOptions. It is bound, appears in --help, and appears in the parameter summary automatically:

[Option('c', "confidence", HelpText = "Minimum confidence.", Default = 0.9f)]
public float Confidence { get; set; }

Options and exit codes

Option Meaning
-s, --silent Skip the confirmation prompt.
--hide-params Suppress the parameter summary.
--help Print usage to stdout and exit 0.
--version Print the version to stdout and exit 0.
Code Name Meaning
0 Success The run completed.
1 InvalidArguments The command line could not be parsed.
2 PreRunFailed Pre-run returned false, so the run was skipped.
3 PostRunFailed The run succeeded but post-run failed.
4 UnhandledError An exception escaped the application.
130 Cancelled The prompt was declined, or Ctrl+C was pressed.

Program output goes to stdout; logs, usage printed for a bad command line, and stack traces go to stderr. MyTool | something is therefore never corrupted.

Building and testing this template

Work on the template in place with the solution at its root:

dotnet build Src-DotNet-ConsoleDiCmdLine/ConsoleTemplate.sln -c Release
dotnet test  Src-DotNet-ConsoleDiCmdLine.Tests/ConsoleTemplate.Tests.csproj -c Release

The test project covers the run lifecycle, each failure path, cancellation, and stdout/stderr separation.

Running it in a container

docker build -t consoletemplate ./Src-DotNet-ConsoleDiCmdLine
docker run --rm consoletemplate --version
docker run --rm consoletemplate -s

The image runs as the non-root user the .NET base image provides. Because no terminal is attached, the confirmation prompt does not appear and the exit pause is skipped, so the container exits on its own.

If Docker runs inside WSL rather than on Windows, prefix the Docker commands with wsl -e, for example wsl -e docker build -t consoletemplate ./Src-DotNet-ConsoleDiCmdLine.

Uninstalling

dotnet new uninstall ./Src-DotNet-ConsoleDiCmdLine

About

Templates

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages