https://github.com/specstet/consoleappwitharguments
Basic template for a console application with start arguments
https://github.com/specstet/consoleappwitharguments
arguments console-app console-application dotnet dotnet-core dotnet8 dotnetcore parameters
Last synced: 4 months ago
JSON representation
Basic template for a console application with start arguments
- Host: GitHub
- URL: https://github.com/specstet/consoleappwitharguments
- Owner: SPECSTET
- Created: 2024-02-14T09:51:34.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-14T11:48:32.000Z (over 2 years ago)
- Last Synced: 2025-09-15T08:42:06.059Z (9 months ago)
- Topics: arguments, console-app, console-application, dotnet, dotnet-core, dotnet8, dotnetcore, parameters
- Language: C#
- Homepage: https://www.specstet.com/
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Console app with arguments or env variables
> Template for a C# console application that can be started with arguments or environment variables.
> It can be provided with default values or with an error message if the value is not set as a parameter or as an environment variable.
> Vorlage für eine C# Konsolen Applikation, die mit Argumenten oder mit Umgebungsvariablen gestartet werden kann.
> Sie kann mit Defaultwerten versehen werden oder mit einer Fehlermeldung, wenn der Wert nicht als Parameter oder als Umgebungsvariable gesetzt ist.
> Шаблон для консольного приложения на C#, которое можно запускать с помощью аргументов или переменных окружения.
> Он может иметь значения по умолчанию или сообщение об ошибке, если значение не задано в качестве параметра или переменной окружения.
The template is based on [System.CommandLine](https://www.nuget.org/packages/System.CommandLine) from Microsoft.
```csharp
using System.CommandLine;
var prefix = "TRXPARSER";
// Title with default value as string
var title = new Option
(name: "--title",
description: "The title of the test run",
getDefaultValue: () => Environment.GetEnvironmentVariable($"{prefix}_TITLE") ?? "Title argument not set!");
// Searchpath with exception when not set
var searchpath = new Option
(name: "--searchpath",
description: "File path to search the TRX files",
getDefaultValue: () => Environment.GetEnvironmentVariable($"{prefix}_SEARCHPATH") ?? throw new Exception("\n\n=>Search path for TRX files not set!\n"));
// OKLimit with default value as int
var oklimit = new Option
(name: "--oklimit",
description: "Minimum percentage as Int for a test to be marked as OK",
getDefaultValue: () => int.TryParse(Environment.GetEnvironmentVariable($"{prefix}_OKLIMIT"), out int limit) ? limit : 100);
var rootCommand = new RootCommand
{
title,
searchpath,
oklimit
};
rootCommand.SetHandler((title, searchpath, oklimit) =>
{
Console.WriteLine(searchpath);
Console.WriteLine(oklimit);
Console.WriteLine(title);
}, title, searchpath, oklimit);
await rootCommand.InvokeAsync(args);
```