Mit dem Namespace System.CommandLine geht das sehr komfortabel. Hier ein Beispiel.
using System.CommandLine;
var apiKeyOption = new Option<string?>(
name: "--api-key",
description: "API key (optional)"
);
var apiBaseOption = new Option<string>(
name: "--api-base",
getDefaultValue: () => "http://localhost/v1",
description: "API base URL"
);
var root = new RootCommand("My tool")
{
apiKeyOption,
apiBaseOption
};
root.SetHandler((string? apiKey, string apiBase) =>
{
Console.WriteLine($"apiBase={apiBase}");
Console.WriteLine($"apiKey={(string.IsNullOrEmpty(apiKey) ? "(leer)" : "***")}");
}, apiKeyOption, apiBaseOption);
return await root.InvokeAsync(args);