| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using CustomHostingDemo.Logging;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.CommandLine;
- using System.CommandLine.Invocation;
- 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 : IDefaultArgs
- {
- public DateTime Date { get; set; }
- public bool Verbose { get; set; }
- }
- public class DefaultHandler : ICommandHandler<HelloArgs>
- {
- private readonly ICliLogger _logger;
- private readonly IConfiguration _config;
- public DefaultHandler(ICliLogger logger, IConfiguration config)
- {
- _logger = logger;
- _config = config;
- }
- public Task<int> InvokeAsync(InvocationContext context, HelloArgs args)
- {
- _logger.LogContent($"Hello {_config["Hello"]}! How are you?");
- _logger.LogInfo($"P: --date {args.Date}");
- _logger.LogInfo($"P: --verbose {args.Verbose}");
- _logger.LogWarning("This is your last warning!");
- _logger.LogError("Just kidding. This is a test.");
- return Task.FromResult(0);
- }
- }
- }
- }
|