HelloCommand.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Ninject.Syntax;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.CommandLine;
  5. using System.CommandLine.Invocation;
  6. using System.CommandLine.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace CustomHostingDemo
  11. {
  12. public class HelloCommand : Command
  13. {
  14. public HelloCommand()
  15. : base("hello", "says hello")
  16. {
  17. Handler = CommandHandler.Create(typeof(DefaultHandler).GetMethod(nameof(ICommandHandler.InvokeAsync)));
  18. AddArgument(new Argument<DateTime>("date", () => DateTime.Today));
  19. AddOption(new Option<bool>("--verbose"));
  20. }
  21. public class HelloArgs
  22. {
  23. public DateTime Date { get; set; }
  24. public bool Verbose { get; set; }
  25. }
  26. public class DefaultHandler : ICommandHandler
  27. {
  28. private readonly HelloArgs _args;
  29. private readonly IResolutionRoot _resolutionRoot;
  30. public DefaultHandler(HelloArgs args, IResolutionRoot resolutionRoot)
  31. {
  32. _args = args;
  33. _resolutionRoot = resolutionRoot;
  34. }
  35. public Task<int> InvokeAsync(InvocationContext context)
  36. {
  37. context.Console.Out.WriteLine("Hello YOU!");
  38. context.Console.Out.WriteLine($"P: --date {_args.Date}");
  39. context.Console.Out.WriteLine($"P: --verbose {_args.Verbose}");
  40. return Task.FromResult(0);
  41. }
  42. }
  43. }
  44. }