Implemented AddAsyncDelegate (#766)

This commit is contained in:
Ignacio Calvo 2023-05-25 12:31:01 +02:00 committed by GitHub
parent 0ec70a44db
commit 35ce60b596
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 859 additions and 675 deletions

View File

@ -9,7 +9,7 @@ public static partial class Program
{
[CommandOption("--count")]
[Description("The number of bars to print")]
[DefaultValue(1)]
[DefaultValue(3)]
public int Count { get; set; }
}
}

View File

@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Spectre.Console;
using Spectre.Console.Cli;
@ -14,7 +15,13 @@ public static partial class Program
.WithDescription("Foos the bars");
config.AddDelegate<BarSettings>("bar", Bar)
.WithDescription("Bars the foos"); ;
.WithDescription("Bars the foos");
config.AddAsyncDelegate("fooAsync", FooAsync)
.WithDescription("Foos the bars asynchronously");
config.AddAsyncDelegate<BarSettings>("barAsync", BarAsync)
.WithDescription("Bars the foos asynchronously");
});
return app.Run(args);
@ -35,4 +42,20 @@ public static partial class Program
return 0;
}
private static Task<int> FooAsync(CommandContext context)
{
AnsiConsole.WriteLine("Foo");
return Task.FromResult(0);
}
private static Task<int> BarAsync(CommandContext context, BarSettings settings)
{
for (var index = 0; index < settings.Count; index++)
{
AnsiConsole.WriteLine("Bar");
}
return Task.FromResult(0);
}
}

View File

@ -9,8 +9,8 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Shared\Shared.csproj" />
<ProjectReference Include="..\..\..\src\Spectre.Console.Json\Spectre.Console.Json.csproj" />
<ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup>
</Project>

File diff suppressed because it is too large Load Diff

View File

@ -237,6 +237,26 @@ public static class ConfiguratorExtensions
return configurator.AddDelegate<EmptyCommandSettings>(name, (c, _) => func(c));
}
/// <summary>
/// Adds a command without settings that executes an async delegate.
/// </summary>
/// <param name="configurator">The configurator.</param>
/// <param name="name">The name of the command.</param>
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
public static ICommandConfigurator AddAsyncDelegate(
this IConfigurator configurator,
string name,
Func<CommandContext, Task<int>> func)
{
if (configurator == null)
{
throw new ArgumentNullException(nameof(configurator));
}
return configurator.AddAsyncDelegate<EmptyCommandSettings>(name, (c, _) => func(c));
}
/// <summary>
/// Adds a command without settings that executes a delegate.
/// </summary>
@ -259,6 +279,28 @@ public static class ConfiguratorExtensions
return configurator.AddDelegate<TSettings>(name, (c, _) => func(c));
}
/// <summary>
/// Adds a command without settings that executes an async delegate.
/// </summary>
/// <typeparam name="TSettings">The command setting type.</typeparam>
/// <param name="configurator">The configurator.</param>
/// <param name="name">The name of the command.</param>
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
public static ICommandConfigurator AddAsyncDelegate<TSettings>(
this IConfigurator<TSettings> configurator,
string name,
Func<CommandContext, Task<int>> func)
where TSettings : CommandSettings
{
if (configurator == null)
{
throw new ArgumentNullException(nameof(configurator));
}
return configurator.AddAsyncDelegate<TSettings>(name, (c, _) => func(c));
}
/// <summary>
/// Sets the ExceptionsHandler.
/// <para>Setting <see cref="ICommandAppSettings.ExceptionHandler"/> this way will use the

View File

@ -35,6 +35,16 @@ public interface IConfigurator
ICommandConfigurator AddDelegate<TSettings>(string name, Func<CommandContext, TSettings, int> func)
where TSettings : CommandSettings;
/// <summary>
/// Adds a command that executes an async delegate.
/// </summary>
/// <typeparam name="TSettings">The command setting type.</typeparam>
/// <param name="name">The name of the command.</param>
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
ICommandConfigurator AddAsyncDelegate<TSettings>(string name, Func<CommandContext, TSettings, Task<int>> func)
where TSettings : CommandSettings;
/// <summary>
/// Adds a command branch.
/// </summary>

View File

@ -57,6 +57,16 @@ public interface IConfigurator<in TSettings>
ICommandConfigurator AddDelegate<TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, int> func)
where TDerivedSettings : TSettings;
/// <summary>
/// Adds a command that executes an async delegate.
/// </summary>
/// <typeparam name="TDerivedSettings">The derived command setting type.</typeparam>
/// <param name="name">The name of the command.</param>
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
ICommandConfigurator AddAsyncDelegate<TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, Task<int>> func)
where TDerivedSettings : TSettings;
/// <summary>
/// Adds a command branch.
/// </summary>

View File

@ -23,7 +23,7 @@ internal sealed class Configurator : IUnsafeConfigurator, IConfigurator, IConfig
public void AddExample(params string[] args)
{
Examples.Add(args);
}
}
public ConfiguredCommand SetDefaultCommand<TDefaultCommand>()
where TDefaultCommand : class, ICommand
@ -42,6 +42,14 @@ internal sealed class Configurator : IUnsafeConfigurator, IConfigurator, IConfig
public ICommandConfigurator AddDelegate<TSettings>(string name, Func<CommandContext, TSettings, int> func)
where TSettings : CommandSettings
{
var command = Commands.AddAndReturn(ConfiguredCommand.FromDelegate<TSettings>(
name, (context, settings) => Task.FromResult(func(context, (TSettings)settings))));
return new CommandConfigurator(command);
}
public ICommandConfigurator AddAsyncDelegate<TSettings>(string name, Func<CommandContext, TSettings, Task<int>> func)
where TSettings : CommandSettings
{
var command = Commands.AddAndReturn(ConfiguredCommand.FromDelegate<TSettings>(
name, (context, settings) => func(context, (TSettings)settings)));

View File

@ -1,100 +1,111 @@
namespace Spectre.Console.Cli;
internal sealed class Configurator<TSettings> : IUnsafeBranchConfigurator, IConfigurator<TSettings>
where TSettings : CommandSettings
{
private readonly ConfiguredCommand _command;
private readonly ITypeRegistrar? _registrar;
public Configurator(ConfiguredCommand command, ITypeRegistrar? registrar)
{
_command = command;
_registrar = registrar;
}
public void SetDescription(string description)
{
_command.Description = description;
}
public void AddExample(string[] args)
{
_command.Examples.Add(args);
}
public void SetDefaultCommand<TDefaultCommand>()
where TDefaultCommand : class, ICommandLimiter<TSettings>
{
var defaultCommand = ConfiguredCommand.FromType<TDefaultCommand>(
CliConstants.DefaultCommandName, isDefaultCommand: true);
_command.Children.Add(defaultCommand);
}
public void HideBranch()
{
_command.IsHidden = true;
}
public ICommandConfigurator AddCommand<TCommand>(string name)
where TCommand : class, ICommandLimiter<TSettings>
{
var command = ConfiguredCommand.FromType<TCommand>(name, isDefaultCommand: false);
var configurator = new CommandConfigurator(command);
_command.Children.Add(command);
return configurator;
}
public ICommandConfigurator AddDelegate<TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, int> func)
where TDerivedSettings : TSettings
{
var command = ConfiguredCommand.FromDelegate<TDerivedSettings>(
name, (context, settings) => func(context, (TDerivedSettings)settings));
_command.Children.Add(command);
return new CommandConfigurator(command);
}
public IBranchConfigurator AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
where TDerivedSettings : TSettings
{
var command = ConfiguredCommand.FromBranch<TDerivedSettings>(name);
action(new Configurator<TDerivedSettings>(command, _registrar));
var added = _command.Children.AddAndReturn(command);
return new BranchConfigurator(added);
}
ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
{
var method = GetType().GetMethod("AddCommand");
if (method == null)
{
throw new CommandConfigurationException("Could not find AddCommand by reflection.");
}
method = method.MakeGenericMethod(command);
if (!(method.Invoke(this, new object[] { name }) is ICommandConfigurator result))
{
throw new CommandConfigurationException("Invoking AddCommand returned null.");
}
return result;
}
IBranchConfigurator IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
{
var command = ConfiguredCommand.FromBranch(settings, name);
// Create the configurator.
var configuratorType = typeof(Configurator<>).MakeGenericType(settings);
if (!(Activator.CreateInstance(configuratorType, new object?[] { command, _registrar }) is IUnsafeBranchConfigurator configurator))
{
throw new CommandConfigurationException("Could not create configurator by reflection.");
}
action(configurator);
var added = _command.Children.AddAndReturn(command);
return new BranchConfigurator(added);
}
namespace Spectre.Console.Cli;
internal sealed class Configurator<TSettings> : IUnsafeBranchConfigurator, IConfigurator<TSettings>
where TSettings : CommandSettings
{
private readonly ConfiguredCommand _command;
private readonly ITypeRegistrar? _registrar;
public Configurator(ConfiguredCommand command, ITypeRegistrar? registrar)
{
_command = command;
_registrar = registrar;
}
public void SetDescription(string description)
{
_command.Description = description;
}
public void AddExample(string[] args)
{
_command.Examples.Add(args);
}
public void SetDefaultCommand<TDefaultCommand>()
where TDefaultCommand : class, ICommandLimiter<TSettings>
{
var defaultCommand = ConfiguredCommand.FromType<TDefaultCommand>(
CliConstants.DefaultCommandName, isDefaultCommand: true);
_command.Children.Add(defaultCommand);
}
public void HideBranch()
{
_command.IsHidden = true;
}
public ICommandConfigurator AddCommand<TCommand>(string name)
where TCommand : class, ICommandLimiter<TSettings>
{
var command = ConfiguredCommand.FromType<TCommand>(name, isDefaultCommand: false);
var configurator = new CommandConfigurator(command);
_command.Children.Add(command);
return configurator;
}
public ICommandConfigurator AddDelegate<TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, int> func)
where TDerivedSettings : TSettings
{
var command = ConfiguredCommand.FromDelegate<TDerivedSettings>(
name, (context, settings) => Task.FromResult(func(context, (TDerivedSettings)settings)));
_command.Children.Add(command);
return new CommandConfigurator(command);
}
public ICommandConfigurator AddAsyncDelegate<TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, Task<int>> func)
where TDerivedSettings : TSettings
{
var command = ConfiguredCommand.FromDelegate<TDerivedSettings>(
name, (context, settings) => func(context, (TDerivedSettings)settings));
_command.Children.Add(command);
return new CommandConfigurator(command);
}
public IBranchConfigurator AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
where TDerivedSettings : TSettings
{
var command = ConfiguredCommand.FromBranch<TDerivedSettings>(name);
action(new Configurator<TDerivedSettings>(command, _registrar));
var added = _command.Children.AddAndReturn(command);
return new BranchConfigurator(added);
}
ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
{
var method = GetType().GetMethod("AddCommand");
if (method == null)
{
throw new CommandConfigurationException("Could not find AddCommand by reflection.");
}
method = method.MakeGenericMethod(command);
if (!(method.Invoke(this, new object[] { name }) is ICommandConfigurator result))
{
throw new CommandConfigurationException("Invoking AddCommand returned null.");
}
return result;
}
IBranchConfigurator IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
{
var command = ConfiguredCommand.FromBranch(settings, name);
// Create the configurator.
var configuratorType = typeof(Configurator<>).MakeGenericType(settings);
if (!(Activator.CreateInstance(configuratorType, new object?[] { command, _registrar }) is IUnsafeBranchConfigurator configurator))
{
throw new CommandConfigurationException("Could not create configurator by reflection.");
}
action(configurator);
var added = _command.Children.AddAndReturn(command);
return new BranchConfigurator(added);
}
}

View File

@ -8,7 +8,7 @@ internal sealed class ConfiguredCommand
public object? Data { get; set; }
public Type? CommandType { get; }
public Type SettingsType { get; }
public Func<CommandContext, CommandSettings, int>? Delegate { get; }
public Func<CommandContext, CommandSettings, Task<int>>? Delegate { get; }
public bool IsDefaultCommand { get; }
public bool IsHidden { get; set; }
@ -19,7 +19,7 @@ internal sealed class ConfiguredCommand
string name,
Type? commandType,
Type settingsType,
Func<CommandContext, CommandSettings, int>? @delegate,
Func<CommandContext, CommandSettings, Task<int>>? @delegate,
bool isDefaultCommand)
{
Name = name;
@ -27,10 +27,10 @@ internal sealed class ConfiguredCommand
CommandType = commandType;
SettingsType = settingsType;
Delegate = @delegate;
IsDefaultCommand = isDefaultCommand;
IsDefaultCommand = isDefaultCommand;
// Default commands are always created as hidden.
IsHidden = IsDefaultCommand;
IsHidden = IsDefaultCommand;
Children = new List<ConfiguredCommand>();
Examples = new List<string[]>();
@ -60,8 +60,8 @@ internal sealed class ConfiguredCommand
}
public static ConfiguredCommand FromDelegate<TSettings>(
string name, Func<CommandContext, CommandSettings, int>? @delegate = null)
where TSettings : CommandSettings
string name, Func<CommandContext, CommandSettings, Task<int>>? @delegate = null)
where TSettings : CommandSettings
{
return new ConfiguredCommand(name, null, typeof(TSettings), @delegate, false);
}

View File

@ -2,16 +2,16 @@ namespace Spectre.Console.Cli;
internal sealed class DelegateCommand : ICommand
{
private readonly Func<CommandContext, CommandSettings, int> _func;
private readonly Func<CommandContext, CommandSettings, Task<int>> _func;
public DelegateCommand(Func<CommandContext, CommandSettings, int> func)
public DelegateCommand(Func<CommandContext, CommandSettings, Task<int>> func)
{
_func = func;
}
public Task<int> Execute(CommandContext context, CommandSettings settings)
{
return Task.FromResult(_func(context, settings));
return _func(context, settings);
}
public ValidationResult Validate(CommandContext context, CommandSettings settings)

View File

@ -1,14 +1,14 @@
namespace Spectre.Console.Cli;
internal sealed class CommandInfo : ICommandContainer
{
{
public string Name { get; }
public HashSet<string> Aliases { get; }
public string? Description { get; }
public object? Data { get; }
public Type? CommandType { get; }
public Type SettingsType { get; }
public Func<CommandContext, CommandSettings, int>? Delegate { get; }
public Func<CommandContext, CommandSettings, Task<int>>? Delegate { get; }
public bool IsDefaultCommand { get; }
public CommandInfo? Parent { get; }
public IList<CommandInfo> Children { get; }
@ -16,11 +16,11 @@ internal sealed class CommandInfo : ICommandContainer
public IList<string[]> Examples { get; }
public bool IsBranch => CommandType == null && Delegate == null;
IList<CommandInfo> ICommandContainer.Commands => Children;
IList<CommandInfo> ICommandContainer.Commands => Children;
// only branches can have a default command
public CommandInfo? DefaultCommand => IsBranch ? Children.FirstOrDefault(c => c.IsDefaultCommand) : null;
public bool IsHidden { get; }
public CommandInfo? DefaultCommand => IsBranch ? Children.FirstOrDefault(c => c.IsDefaultCommand) : null;
public bool IsHidden { get; }
public CommandInfo(CommandInfo? parent, ConfiguredCommand prototype)
{

View File

@ -1127,6 +1127,37 @@ public sealed partial class CommandAppTests
// When
var result = app.Run(new[] { "foo", "4", "12" });
// Then
result.ShouldBe(1);
dog.ShouldNotBeNull();
dog.Age.ShouldBe(12);
dog.Legs.ShouldBe(4);
data.ShouldBe(2);
}
[Fact]
public async void Should_Execute_Async_Delegate_Command_At_Root_Level()
{
// Given
var dog = default(DogSettings);
var data = 0;
var app = new CommandApp();
app.Configure(config =>
{
config.PropagateExceptions();
config.AddAsyncDelegate<DogSettings>(
"foo", (context, settings) =>
{
dog = settings;
data = (int)context.Data;
return Task.FromResult(1);
}).WithData(2);
});
// When
var result = await app.RunAsync(new[] { "foo", "4", "12" });
// Then
result.ShouldBe(1);
dog.ShouldNotBeNull();
@ -1161,6 +1192,40 @@ public sealed partial class CommandAppTests
// When
var result = app.Run(new[] { "foo", "4", "bar", "12" });
// Then
result.ShouldBe(1);
dog.ShouldNotBeNull();
dog.Age.ShouldBe(12);
dog.Legs.ShouldBe(4);
data.ShouldBe(2);
}
[Fact]
public async void Should_Execute_Nested_Async_Delegate_Command()
{
// Given
var dog = default(DogSettings);
var data = 0;
var app = new CommandApp();
app.Configure(config =>
{
config.PropagateExceptions();
config.AddBranch<AnimalSettings>("foo", foo =>
{
foo.AddAsyncDelegate<DogSettings>(
"bar", (context, settings) =>
{
dog = settings;
data = (int)context.Data;
return Task.FromResult(1);
}).WithData(2);
});
});
// When
var result = await app.RunAsync(new[] { "foo", "4", "bar", "12" });
// Then
result.ShouldBe(1);
dog.ShouldNotBeNull();