| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using Ninject.Syntax;
- using System;
- using System.Collections.Generic;
- using System.CommandLine;
- using System.CommandLine.Invocation;
- using System.CommandLine.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CustomHostingDemo
- {
- public class HelloCommand : Command
- {
- public HelloCommand()
- : base("hello", "says hello")
- {
- Handler = CommandHandler.Create(typeof(DefaultHandler).GetMethod(nameof(ICommandHandler.InvokeAsync)));
- 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
- {
- private readonly HelloArgs _args;
- private readonly IResolutionRoot _resolutionRoot;
- public DefaultHandler(HelloArgs args, IResolutionRoot resolutionRoot)
- {
- _args = args;
- _resolutionRoot = resolutionRoot;
- }
- public Task<int> InvokeAsync(InvocationContext context)
- {
- context.Console.Out.WriteLine("Hello YOU!");
- context.Console.Out.WriteLine($"P: --date {_args.Date}");
- context.Console.Out.WriteLine($"P: --verbose {_args.Verbose}");
- return Task.FromResult(0);
- }
- }
- }
- }
|