Skip to content

Commit

Permalink
Workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingpie committed Dec 11, 2024
1 parent 1970ed8 commit 70beb0c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 15 deletions.
5 changes: 5 additions & 0 deletions DeclarativeCommandLine.TestApp/Commands/AbsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
[Command]
public class AbsCommand : ICommand
{
public AbsCommand()
{
Console.WriteLine($"new {GetType().FullName}()");
}

[Option(IsRequired = true)]
public int NumberA { get; set; }

Expand Down
5 changes: 5 additions & 0 deletions DeclarativeCommandLine.TestApp/Commands/AddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
[Command]
public class AddCommand : ICommand
{
public AddCommand()
{
Console.WriteLine($"new {GetType().FullName}()");
}

[Option(IsRequired = true)]
public int NumberA { get; set; }

Expand Down
5 changes: 5 additions & 0 deletions DeclarativeCommandLine.TestApp/Commands/DivideCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
[Command]
public class DivideCommand : ICommand
{
public DivideCommand()
{
Console.WriteLine($"new {GetType().FullName}()");
}

[Option(IsRequired = true)]
public int NumberA { get; set; }

Expand Down
5 changes: 5 additions & 0 deletions DeclarativeCommandLine.TestApp/Commands/SubtractCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
[Command]
public class SubtractCommand : ICommand
{
public SubtractCommand()
{
Console.WriteLine($"new {GetType().FullName}()");
}

[Argument]
public int ValueA { get; set; }

Expand Down
4 changes: 4 additions & 0 deletions DeclarativeCommandLine.TestApp/Commands/TestRootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
[RootCommand]
public class TestRootCommand
{
public TestRootCommand()
{
Console.WriteLine($"new {GetType().FullName}()");
}
}
13 changes: 8 additions & 5 deletions DeclarativeCommandLine/DeclarativeCommandLineFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public async Task<int> InvokeAsync(string[] args)

private Command BuildCommandTree(IEnumerable<CommandDescriptor> 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()
Expand All @@ -112,7 +112,7 @@ private Command BuildCommandTree(IEnumerable<CommandDescriptor> 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);

Expand Down Expand Up @@ -161,7 +161,10 @@ private Command BuildCommandTree(IEnumerable<CommandDescriptor> cmdDescrs, Comma
{
cmdDescr.Command.SetHandler(new Func<InvocationContext, Task<int>>(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);
}));
}

Expand Down
2 changes: 1 addition & 1 deletion DeclarativeCommandLine/Utils/CommandDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
18 changes: 9 additions & 9 deletions DeclarativeCommandLine/Utils/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public CommandHandler(
//_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
}

public async Task<int> HandlerAsync(InvocationContext ctx)
public async Task<int> HandlerAsync(object cmdInst, InvocationContext ctx)
{
if (ctx == null)
{
Expand All @@ -26,19 +26,19 @@ public async Task<int> 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;
Expand Down Expand Up @@ -83,21 +83,21 @@ public async Task<int> 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);
}
}

Expand Down

0 comments on commit 70beb0c

Please sign in to comment.