HelloCommand.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.CommandLine;
  4. using System.CommandLine.Invocation;
  5. using System.CommandLine.IO;
  6. using System.CommandLine.Rendering;
  7. using System.Threading.Tasks;
  8. namespace CustomHostingDemo
  9. {
  10. public class HelloCommand : CustomCommand<HelloCommand.HelloArgs>
  11. {
  12. public HelloCommand()
  13. : base("hello", "says hello", typeof(DefaultHandler))
  14. {
  15. AddArgument(new Argument<DateTime>("date", () => DateTime.Today));
  16. AddOption(new Option<bool>("--verbose"));
  17. }
  18. public class HelloArgs
  19. {
  20. public DateTime Date { get; set; }
  21. public bool Verbose { get; set; }
  22. }
  23. public class DefaultHandler : ICommandHandler<HelloArgs>
  24. {
  25. private readonly ILogger _logger;
  26. public DefaultHandler(Logger<DefaultHandler> logger)
  27. {
  28. _logger = logger;
  29. }
  30. public Task<int> InvokeAsync(InvocationContext context, HelloArgs args)
  31. {
  32. context.Console.Out.WriteLine("Hello YOU!");
  33. context.Console.Out.WriteLine($"P: --date {args.Date}");
  34. context.Console.Out.WriteLine($"P: --verbose {args.Verbose}");
  35. _logger.LogInformation("INFO from DefaultHandler");
  36. var span = new ContainerSpan(StyleSpan.UnderlinedOn(),
  37. new ContentSpan("Underlining"),
  38. StyleSpan.UnderlinedOff());
  39. context.Console.Out.WriteLine(span.ToString(OutputMode.Auto));
  40. context.Console.Out.WriteLine(new ContainerSpan(
  41. ForegroundColorSpan.LightGreen(),
  42. StyleSpan.BlinkOn(),
  43. new ContentSpan("Blinking"),
  44. StyleSpan.BlinkOff(),
  45. new ContentSpan("Noblink"),
  46. ForegroundColorSpan.Reset(),
  47. new ContentSpan("Normal")
  48. ).ToString(OutputMode.Ansi));
  49. return Task.FromResult(3);
  50. }
  51. }
  52. }
  53. }