HelloCommand.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. AddOption(new Option<bool>("--verbose"));
  19. }
  20. public class DefaultHandler : ICommandHandler
  21. {
  22. private readonly IResolutionRoot _resolutionRoot;
  23. public bool Verbose { get; set; }
  24. public DefaultHandler(IResolutionRoot resolutionRoot)
  25. {
  26. _resolutionRoot = resolutionRoot;
  27. }
  28. public Task<int> InvokeAsync(InvocationContext context)
  29. {
  30. context.Console.Out.WriteLine("Hello YOU!");
  31. context.Console.Out.WriteLine($"C: --verbose {Verbose}");
  32. return Task.FromResult(0);
  33. }
  34. }
  35. }
  36. }