Lukas Angerer 4 лет назад
Родитель
Сommit
8293f75d60

+ 12 - 4
CustomHostingDemo/HelloCommand.cs

@@ -16,24 +16,32 @@ namespace CustomHostingDemo
             : 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 bool Verbose { get; set; }
-
-            public DefaultHandler(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($"C: --verbose {Verbose}");
+                context.Console.Out.WriteLine($"P: --date {_args.Date}");
+                context.Console.Out.WriteLine($"P: --verbose {_args.Verbose}");
 
                 return Task.FromResult(0);
             }

+ 9 - 9
CustomHostingDemo/Program.cs

@@ -44,23 +44,23 @@ namespace CustomHostingDemo
             var parser = new CommandLineBuilder(rootCommand)
                 .UseMiddleware((invocationContext) =>
                 {
-                    //invocationContext.BindingContext.ParseResult.
-                    //invocationContext.BindingContext.AddService(typeof(IResolutionRoot), () => kernel);
+                    var cmdArgs = new HelloCommand.HelloArgs();
+                    var binder = new ModelBinder<HelloCommand.HelloArgs>();
+                    binder.UpdateInstance(cmdArgs, invocationContext.BindingContext);
+
+                    kernel.Bind<HelloCommand.HelloArgs>().ToConstant(cmdArgs).InSingletonScope();
+
+                    //invocationContext.BindingContext.AddService(typeof(HelloCommand.HelloArgs), () => cmdArgs);
                     invocationContext.BindingContext.AddService(typeof(HelloCommand.DefaultHandler), () =>
                     {
                         var handler = kernel.Get<HelloCommand.DefaultHandler>();
-                        var binder = new ModelBinder<HelloCommand.DefaultHandler>();
-                        binder.UpdateInstance(handler, invocationContext.BindingContext);
                         return handler;
                     });
-                    //invocationContext.BindingContext.AddService(typeof(HelloCommand), () => {
-                    //    return kernel.Get<HelloCommand>();
-                    //});
                 })
+                .UseDefaults()
                 .Build();
 
-            parser.InvokeAsync(args);
-            //rootCommand.Invoke(args);
+            parser.InvokeAsync(args).Wait();
         }
     }
 }

+ 1 - 1
CustomHostingDemo/Properties/launchSettings.json

@@ -2,7 +2,7 @@
   "profiles": {
     "CustomHostingDemo": {
       "commandName": "Project",
-      "commandLineArgs": "hello --verbose"
+      "commandLineArgs": "hello --verbose 2021-08-08"
     }
   }
 }