/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/standard/commandline/snippets/use-middleware/csharp/Program.cs
50 строк
1 KB
Peter Ritchie
Fixed csharp middleware example to use args (#30292)
20 июл 2022, 21:11
Не верифицирован
20 июл 2022, 21:11
e14ca3d
Код
Авторство
О чём код?
// <all> using System.CommandLine; using System.CommandLine.Builder; using System.CommandLine.Parsing; class Program { static async Task Main(string[] args) { var delayOption = new Option<int>("--delay"); var messageOption = new Option<string>("--message"); var rootCommand = new RootCommand("Middleware example"); rootCommand.Add(delayOption); rootCommand.Add(messageOption); rootCommand.SetHandler((delayOptionValue, messageOptionValue) => { DoRootCommand(delayOptionValue, messageOptionValue); }, delayOption, messageOption); // <middleware> var commandLineBuilder = new CommandLineBuilder(rootCommand); commandLineBuilder.AddMiddleware(async (context, next) => { if (context.ParseResult.Directives.Contains("just-say-hi")) { context.Console.WriteLine("Hi!"); } else { await next(context); } }); commandLineBuilder.UseDefaults(); var parser = commandLineBuilder.Build(); await parser.InvokeAsync(args); // </middleware> } public static void DoRootCommand(int delay, string message) { Console.WriteLine($"--delay = {delay}"); Console.WriteLine($"--message = {message}"); } } // </all>