| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using Microsoft.Extensions.Logging;
- using System;
- using System.CommandLine;
- using System.CommandLine.Invocation;
- using System.CommandLine.IO;
- using System.CommandLine.Rendering;
- using System.Threading.Tasks;
- namespace CustomHostingDemo
- {
- public class HelloCommand : CustomCommand<HelloCommand.HelloArgs>
- {
- public HelloCommand()
- : base("hello", "says hello", typeof(DefaultHandler))
- {
- AddArgument(new Argument<DateTime>("date", () => DateTime.Today));
- AddOption(new Option<bool>("--verbose"));
- }
- public class HelloArgs
- {
- public DateTime Date { get; set; }
- public bool Verbose { get; set; }
- }
- public class DefaultHandler : ICommandHandler<HelloArgs>
- {
- private readonly ILogger _logger;
- public DefaultHandler(Logger<DefaultHandler> logger)
- {
- _logger = logger;
- }
- public Task<int> InvokeAsync(InvocationContext context, HelloArgs args)
- {
- context.Console.Out.WriteLine("Hello YOU!");
- context.Console.Out.WriteLine($"P: --date {args.Date}");
- context.Console.Out.WriteLine($"P: --verbose {args.Verbose}");
- _logger.LogInformation("INFO from DefaultHandler");
- return Task.FromResult(3);
- }
- }
- }
- }
|