diff --git a/DeclarativeCommandLine.TestApp/Commands/AbsCommand.cs b/DeclarativeCommandLine.TestApp/Commands/AbsCommand.cs index cc34cbc..ca33d31 100644 --- a/DeclarativeCommandLine.TestApp/Commands/AbsCommand.cs +++ b/DeclarativeCommandLine.TestApp/Commands/AbsCommand.cs @@ -3,6 +3,11 @@ [Command] public class AbsCommand : ICommand { + public AbsCommand() + { + Console.WriteLine($"new {GetType().FullName}()"); + } + [Option(IsRequired = true)] public int NumberA { get; set; } diff --git a/DeclarativeCommandLine.TestApp/Commands/AddCommand.cs b/DeclarativeCommandLine.TestApp/Commands/AddCommand.cs index ce3db0e..e44c6a0 100644 --- a/DeclarativeCommandLine.TestApp/Commands/AddCommand.cs +++ b/DeclarativeCommandLine.TestApp/Commands/AddCommand.cs @@ -3,6 +3,11 @@ [Command] public class AddCommand : ICommand { + public AddCommand() + { + Console.WriteLine($"new {GetType().FullName}()"); + } + [Option(IsRequired = true)] public int NumberA { get; set; } diff --git a/DeclarativeCommandLine.TestApp/Commands/DivideCommand.cs b/DeclarativeCommandLine.TestApp/Commands/DivideCommand.cs index 9223380..cf132ff 100644 --- a/DeclarativeCommandLine.TestApp/Commands/DivideCommand.cs +++ b/DeclarativeCommandLine.TestApp/Commands/DivideCommand.cs @@ -3,6 +3,11 @@ [Command] public class DivideCommand : ICommand { + public DivideCommand() + { + Console.WriteLine($"new {GetType().FullName}()"); + } + [Option(IsRequired = true)] public int NumberA { get; set; } diff --git a/DeclarativeCommandLine.TestApp/Commands/SubtractCommand.cs b/DeclarativeCommandLine.TestApp/Commands/SubtractCommand.cs index 2f70923..4fd7cf1 100644 --- a/DeclarativeCommandLine.TestApp/Commands/SubtractCommand.cs +++ b/DeclarativeCommandLine.TestApp/Commands/SubtractCommand.cs @@ -3,6 +3,11 @@ [Command] public class SubtractCommand : ICommand { + public SubtractCommand() + { + Console.WriteLine($"new {GetType().FullName}()"); + } + [Argument] public int ValueA { get; set; } diff --git a/DeclarativeCommandLine.TestApp/Commands/TestRootCommand.cs b/DeclarativeCommandLine.TestApp/Commands/TestRootCommand.cs index 079068b..bdcffdc 100644 --- a/DeclarativeCommandLine.TestApp/Commands/TestRootCommand.cs +++ b/DeclarativeCommandLine.TestApp/Commands/TestRootCommand.cs @@ -3,4 +3,8 @@ [RootCommand] public class TestRootCommand { + public TestRootCommand() + { + Console.WriteLine($"new {GetType().FullName}()"); + } } \ No newline at end of file diff --git a/DeclarativeCommandLine/DeclarativeCommandLineFactory.cs b/DeclarativeCommandLine/DeclarativeCommandLineFactory.cs index bd999dc..fcf943b 100644 --- a/DeclarativeCommandLine/DeclarativeCommandLineFactory.cs +++ b/DeclarativeCommandLine/DeclarativeCommandLineFactory.cs @@ -90,10 +90,10 @@ public async Task InvokeAsync(string[] args) private Command BuildCommandTree(IEnumerable cmdDescrs, CommandDescriptor cmdDescr) { - var cmdInst = _commandFactory.CreateCommand(cmdDescr.Type) - ?? throw new InvalidOperationException($"Could not create an instance of type '{cmdDescr.Type}'"); + // var cmdInst = _commandFactory.CreateCommand(cmdDescr.Type) + // ?? throw new InvalidOperationException($"Could not create an instance of type '{cmdDescr.Type}'"); - cmdDescr.Instance = cmdInst; + // cmdDescr.Instance = cmdInst; var cmd = cmdDescr.IsRoot ? new RootCommand() @@ -112,7 +112,7 @@ private Command BuildCommandTree(IEnumerable cmdDescrs, Comma // TODO: Aliases, Arguments, Subcommands, Handler, IsHidden, Name, TreatUnmatchedTokensAsErrors // Options - var props = cmdInst.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); + var props = cmdDescr.Type.GetProperties(BindingFlags.Instance | BindingFlags.Public); var methods = cmdDescr.Type.GetMethods(BindingFlags.Instance | BindingFlags.Public); @@ -161,7 +161,10 @@ private Command BuildCommandTree(IEnumerable cmdDescrs, Comma { cmdDescr.Command.SetHandler(new Func>(async ctx => { - return await new CommandHandler(cmdDescr).HandlerAsync(ctx).ConfigureAwait(false); + var cmdInst = _commandFactory.CreateCommand(cmdDescr.Type) + ?? throw new InvalidOperationException($"Could not create an instance of type '{cmdDescr.Type}'"); + + return await new CommandHandler(cmdDescr).HandlerAsync(cmdInst, ctx).ConfigureAwait(false); })); } diff --git a/DeclarativeCommandLine/Utils/CommandDescriptor.cs b/DeclarativeCommandLine/Utils/CommandDescriptor.cs index 9ec23ca..a6860a5 100644 --- a/DeclarativeCommandLine/Utils/CommandDescriptor.cs +++ b/DeclarativeCommandLine/Utils/CommandDescriptor.cs @@ -49,7 +49,7 @@ public bool IsExecutable public bool IsRoot { get; } - public object? Instance { get; set; } + private object? Instance { get; set; } public CommandDescriptor Parent { get; set; } diff --git a/DeclarativeCommandLine/Utils/CommandHandler.cs b/DeclarativeCommandLine/Utils/CommandHandler.cs index 03ec736..45436ec 100644 --- a/DeclarativeCommandLine/Utils/CommandHandler.cs +++ b/DeclarativeCommandLine/Utils/CommandHandler.cs @@ -16,7 +16,7 @@ public CommandHandler( //_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); } - public async Task HandlerAsync(InvocationContext ctx) + public async Task HandlerAsync(object cmdInst, InvocationContext ctx) { if (ctx == null) { @@ -26,19 +26,19 @@ public async Task HandlerAsync(InvocationContext ctx) //var methodParams = _methodInfo.GetParameters(); // Arguments - HandleArguments(ctx); + HandleArguments(cmdInst, ctx); // Options - HandleOptions(ctx); + HandleOptions(cmdInst, ctx); - if (_cmdDescr.Instance is ICommand cmd) + if (cmdInst is ICommand cmd) { // TODO: Try catch here or somewhere else? cmd.Execute(); return 0; } - if (_cmdDescr.Instance is IAsyncCommand asyncCmd) + if (cmdInst is IAsyncCommand asyncCmd) { await asyncCmd.ExecuteAsync(); return 0; @@ -83,21 +83,21 @@ public async Task HandlerAsync(InvocationContext ctx) // return mp; //} - private void HandleArguments(InvocationContext ctx) + private void HandleArguments(object cmdInst, InvocationContext ctx) { foreach (var arg in _cmdDescr.Arguments) { var argVal = ctx.ParseResult.GetValueForArgument(arg.Argument); - arg.Property.SetValue(_cmdDescr.Instance, argVal); + arg.Property.SetValue(cmdInst, argVal); } } - private void HandleOptions(InvocationContext ctx) + private void HandleOptions(object cmdInst, InvocationContext ctx) { foreach (var opt1 in _cmdDescr.Options) { var optVal = ctx.ParseResult.GetValueForOption(opt1.Option); - opt1.Property.SetValue(_cmdDescr.Instance, optVal); + opt1.Property.SetValue(cmdInst, optVal); } }