mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-20 02:32:50 +08:00
Implemented AddAsyncDelegate
(#766)
This commit is contained in:
parent
0ec70a44db
commit
35ce60b596
@ -9,7 +9,7 @@ public static partial class Program
|
|||||||
{
|
{
|
||||||
[CommandOption("--count")]
|
[CommandOption("--count")]
|
||||||
[Description("The number of bars to print")]
|
[Description("The number of bars to print")]
|
||||||
[DefaultValue(1)]
|
[DefaultValue(3)]
|
||||||
public int Count { get; set; }
|
public int Count { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
using Spectre.Console;
|
using Spectre.Console;
|
||||||
using Spectre.Console.Cli;
|
using Spectre.Console.Cli;
|
||||||
|
|
||||||
@ -14,7 +15,13 @@ public static partial class Program
|
|||||||
.WithDescription("Foos the bars");
|
.WithDescription("Foos the bars");
|
||||||
|
|
||||||
config.AddDelegate<BarSettings>("bar", Bar)
|
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);
|
return app.Run(args);
|
||||||
@ -35,4 +42,20 @@ public static partial class Program
|
|||||||
|
|
||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\Shared\Shared.csproj" />
|
|
||||||
<ProjectReference Include="..\..\..\src\Spectre.Console.Json\Spectre.Console.Json.csproj" />
|
<ProjectReference Include="..\..\..\src\Spectre.Console.Json\Spectre.Console.Json.csproj" />
|
||||||
|
<ProjectReference Include="..\..\Shared\Shared.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -237,6 +237,26 @@ public static class ConfiguratorExtensions
|
|||||||
return configurator.AddDelegate<EmptyCommandSettings>(name, (c, _) => func(c));
|
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>
|
/// <summary>
|
||||||
/// Adds a command without settings that executes a delegate.
|
/// Adds a command without settings that executes a delegate.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -259,6 +279,28 @@ public static class ConfiguratorExtensions
|
|||||||
return configurator.AddDelegate<TSettings>(name, (c, _) => func(c));
|
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>
|
/// <summary>
|
||||||
/// Sets the ExceptionsHandler.
|
/// Sets the ExceptionsHandler.
|
||||||
/// <para>Setting <see cref="ICommandAppSettings.ExceptionHandler"/> this way will use the
|
/// <para>Setting <see cref="ICommandAppSettings.ExceptionHandler"/> this way will use the
|
||||||
|
@ -35,6 +35,16 @@ public interface IConfigurator
|
|||||||
ICommandConfigurator AddDelegate<TSettings>(string name, Func<CommandContext, TSettings, int> func)
|
ICommandConfigurator AddDelegate<TSettings>(string name, Func<CommandContext, TSettings, int> func)
|
||||||
where TSettings : CommandSettings;
|
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>
|
/// <summary>
|
||||||
/// Adds a command branch.
|
/// Adds a command branch.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -57,6 +57,16 @@ public interface IConfigurator<in TSettings>
|
|||||||
ICommandConfigurator AddDelegate<TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, int> func)
|
ICommandConfigurator AddDelegate<TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, int> func)
|
||||||
where TDerivedSettings : TSettings;
|
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>
|
/// <summary>
|
||||||
/// Adds a command branch.
|
/// Adds a command branch.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -23,7 +23,7 @@ internal sealed class Configurator : IUnsafeConfigurator, IConfigurator, IConfig
|
|||||||
public void AddExample(params string[] args)
|
public void AddExample(params string[] args)
|
||||||
{
|
{
|
||||||
Examples.Add(args);
|
Examples.Add(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConfiguredCommand SetDefaultCommand<TDefaultCommand>()
|
public ConfiguredCommand SetDefaultCommand<TDefaultCommand>()
|
||||||
where TDefaultCommand : class, ICommand
|
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)
|
public ICommandConfigurator AddDelegate<TSettings>(string name, Func<CommandContext, TSettings, int> func)
|
||||||
where TSettings : CommandSettings
|
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>(
|
var command = Commands.AddAndReturn(ConfiguredCommand.FromDelegate<TSettings>(
|
||||||
name, (context, settings) => func(context, (TSettings)settings)));
|
name, (context, settings) => func(context, (TSettings)settings)));
|
||||||
|
@ -1,100 +1,111 @@
|
|||||||
namespace Spectre.Console.Cli;
|
namespace Spectre.Console.Cli;
|
||||||
|
|
||||||
internal sealed class Configurator<TSettings> : IUnsafeBranchConfigurator, IConfigurator<TSettings>
|
internal sealed class Configurator<TSettings> : IUnsafeBranchConfigurator, IConfigurator<TSettings>
|
||||||
where TSettings : CommandSettings
|
where TSettings : CommandSettings
|
||||||
{
|
{
|
||||||
private readonly ConfiguredCommand _command;
|
private readonly ConfiguredCommand _command;
|
||||||
private readonly ITypeRegistrar? _registrar;
|
private readonly ITypeRegistrar? _registrar;
|
||||||
|
|
||||||
public Configurator(ConfiguredCommand command, ITypeRegistrar? registrar)
|
public Configurator(ConfiguredCommand command, ITypeRegistrar? registrar)
|
||||||
{
|
{
|
||||||
_command = command;
|
_command = command;
|
||||||
_registrar = registrar;
|
_registrar = registrar;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetDescription(string description)
|
public void SetDescription(string description)
|
||||||
{
|
{
|
||||||
_command.Description = description;
|
_command.Description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddExample(string[] args)
|
public void AddExample(string[] args)
|
||||||
{
|
{
|
||||||
_command.Examples.Add(args);
|
_command.Examples.Add(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetDefaultCommand<TDefaultCommand>()
|
public void SetDefaultCommand<TDefaultCommand>()
|
||||||
where TDefaultCommand : class, ICommandLimiter<TSettings>
|
where TDefaultCommand : class, ICommandLimiter<TSettings>
|
||||||
{
|
{
|
||||||
var defaultCommand = ConfiguredCommand.FromType<TDefaultCommand>(
|
var defaultCommand = ConfiguredCommand.FromType<TDefaultCommand>(
|
||||||
CliConstants.DefaultCommandName, isDefaultCommand: true);
|
CliConstants.DefaultCommandName, isDefaultCommand: true);
|
||||||
_command.Children.Add(defaultCommand);
|
|
||||||
}
|
_command.Children.Add(defaultCommand);
|
||||||
|
}
|
||||||
public void HideBranch()
|
|
||||||
{
|
public void HideBranch()
|
||||||
_command.IsHidden = true;
|
{
|
||||||
}
|
_command.IsHidden = true;
|
||||||
|
}
|
||||||
public ICommandConfigurator AddCommand<TCommand>(string name)
|
|
||||||
where TCommand : class, ICommandLimiter<TSettings>
|
public ICommandConfigurator AddCommand<TCommand>(string name)
|
||||||
{
|
where TCommand : class, ICommandLimiter<TSettings>
|
||||||
var command = ConfiguredCommand.FromType<TCommand>(name, isDefaultCommand: false);
|
{
|
||||||
var configurator = new CommandConfigurator(command);
|
var command = ConfiguredCommand.FromType<TCommand>(name, isDefaultCommand: false);
|
||||||
|
var configurator = new CommandConfigurator(command);
|
||||||
_command.Children.Add(command);
|
|
||||||
return configurator;
|
_command.Children.Add(command);
|
||||||
}
|
return configurator;
|
||||||
|
}
|
||||||
public ICommandConfigurator AddDelegate<TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, int> func)
|
|
||||||
where TDerivedSettings : TSettings
|
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));
|
var command = ConfiguredCommand.FromDelegate<TDerivedSettings>(
|
||||||
|
name, (context, settings) => Task.FromResult(func(context, (TDerivedSettings)settings)));
|
||||||
_command.Children.Add(command);
|
|
||||||
return new CommandConfigurator(command);
|
_command.Children.Add(command);
|
||||||
}
|
return new CommandConfigurator(command);
|
||||||
|
}
|
||||||
public IBranchConfigurator AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
|
|
||||||
where TDerivedSettings : TSettings
|
public ICommandConfigurator AddAsyncDelegate<TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, Task<int>> func)
|
||||||
{
|
where TDerivedSettings : TSettings
|
||||||
var command = ConfiguredCommand.FromBranch<TDerivedSettings>(name);
|
{
|
||||||
action(new Configurator<TDerivedSettings>(command, _registrar));
|
var command = ConfiguredCommand.FromDelegate<TDerivedSettings>(
|
||||||
var added = _command.Children.AddAndReturn(command);
|
name, (context, settings) => func(context, (TDerivedSettings)settings));
|
||||||
return new BranchConfigurator(added);
|
|
||||||
}
|
_command.Children.Add(command);
|
||||||
|
return new CommandConfigurator(command);
|
||||||
ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
|
}
|
||||||
{
|
|
||||||
var method = GetType().GetMethod("AddCommand");
|
public IBranchConfigurator AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
|
||||||
if (method == null)
|
where TDerivedSettings : TSettings
|
||||||
{
|
{
|
||||||
throw new CommandConfigurationException("Could not find AddCommand by reflection.");
|
var command = ConfiguredCommand.FromBranch<TDerivedSettings>(name);
|
||||||
}
|
action(new Configurator<TDerivedSettings>(command, _registrar));
|
||||||
|
var added = _command.Children.AddAndReturn(command);
|
||||||
method = method.MakeGenericMethod(command);
|
return new BranchConfigurator(added);
|
||||||
|
}
|
||||||
if (!(method.Invoke(this, new object[] { name }) is ICommandConfigurator result))
|
|
||||||
{
|
ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
|
||||||
throw new CommandConfigurationException("Invoking AddCommand returned null.");
|
{
|
||||||
}
|
var method = GetType().GetMethod("AddCommand");
|
||||||
|
if (method == null)
|
||||||
return result;
|
{
|
||||||
}
|
throw new CommandConfigurationException("Could not find AddCommand by reflection.");
|
||||||
|
}
|
||||||
IBranchConfigurator IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
|
|
||||||
{
|
method = method.MakeGenericMethod(command);
|
||||||
var command = ConfiguredCommand.FromBranch(settings, name);
|
|
||||||
|
if (!(method.Invoke(this, new object[] { name }) is ICommandConfigurator result))
|
||||||
// Create the configurator.
|
{
|
||||||
var configuratorType = typeof(Configurator<>).MakeGenericType(settings);
|
throw new CommandConfigurationException("Invoking AddCommand returned null.");
|
||||||
if (!(Activator.CreateInstance(configuratorType, new object?[] { command, _registrar }) is IUnsafeBranchConfigurator configurator))
|
}
|
||||||
{
|
|
||||||
throw new CommandConfigurationException("Could not create configurator by reflection.");
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
action(configurator);
|
IBranchConfigurator IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
|
||||||
var added = _command.Children.AddAndReturn(command);
|
{
|
||||||
return new BranchConfigurator(added);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -8,7 +8,7 @@ internal sealed class ConfiguredCommand
|
|||||||
public object? Data { get; set; }
|
public object? Data { get; set; }
|
||||||
public Type? CommandType { get; }
|
public Type? CommandType { get; }
|
||||||
public Type SettingsType { 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 IsDefaultCommand { get; }
|
||||||
public bool IsHidden { get; set; }
|
public bool IsHidden { get; set; }
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ internal sealed class ConfiguredCommand
|
|||||||
string name,
|
string name,
|
||||||
Type? commandType,
|
Type? commandType,
|
||||||
Type settingsType,
|
Type settingsType,
|
||||||
Func<CommandContext, CommandSettings, int>? @delegate,
|
Func<CommandContext, CommandSettings, Task<int>>? @delegate,
|
||||||
bool isDefaultCommand)
|
bool isDefaultCommand)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
@ -27,10 +27,10 @@ internal sealed class ConfiguredCommand
|
|||||||
CommandType = commandType;
|
CommandType = commandType;
|
||||||
SettingsType = settingsType;
|
SettingsType = settingsType;
|
||||||
Delegate = @delegate;
|
Delegate = @delegate;
|
||||||
IsDefaultCommand = isDefaultCommand;
|
IsDefaultCommand = isDefaultCommand;
|
||||||
|
|
||||||
// Default commands are always created as hidden.
|
// Default commands are always created as hidden.
|
||||||
IsHidden = IsDefaultCommand;
|
IsHidden = IsDefaultCommand;
|
||||||
|
|
||||||
Children = new List<ConfiguredCommand>();
|
Children = new List<ConfiguredCommand>();
|
||||||
Examples = new List<string[]>();
|
Examples = new List<string[]>();
|
||||||
@ -60,8 +60,8 @@ internal sealed class ConfiguredCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ConfiguredCommand FromDelegate<TSettings>(
|
public static ConfiguredCommand FromDelegate<TSettings>(
|
||||||
string name, Func<CommandContext, CommandSettings, int>? @delegate = null)
|
string name, Func<CommandContext, CommandSettings, Task<int>>? @delegate = null)
|
||||||
where TSettings : CommandSettings
|
where TSettings : CommandSettings
|
||||||
{
|
{
|
||||||
return new ConfiguredCommand(name, null, typeof(TSettings), @delegate, false);
|
return new ConfiguredCommand(name, null, typeof(TSettings), @delegate, false);
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,16 @@ namespace Spectre.Console.Cli;
|
|||||||
|
|
||||||
internal sealed class DelegateCommand : ICommand
|
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;
|
_func = func;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<int> Execute(CommandContext context, CommandSettings settings)
|
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)
|
public ValidationResult Validate(CommandContext context, CommandSettings settings)
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
namespace Spectre.Console.Cli;
|
namespace Spectre.Console.Cli;
|
||||||
|
|
||||||
internal sealed class CommandInfo : ICommandContainer
|
internal sealed class CommandInfo : ICommandContainer
|
||||||
{
|
{
|
||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
public HashSet<string> Aliases { get; }
|
public HashSet<string> Aliases { get; }
|
||||||
public string? Description { get; }
|
public string? Description { get; }
|
||||||
public object? Data { get; }
|
public object? Data { get; }
|
||||||
public Type? CommandType { get; }
|
public Type? CommandType { get; }
|
||||||
public Type SettingsType { 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 IsDefaultCommand { get; }
|
||||||
public CommandInfo? Parent { get; }
|
public CommandInfo? Parent { get; }
|
||||||
public IList<CommandInfo> Children { get; }
|
public IList<CommandInfo> Children { get; }
|
||||||
@ -16,11 +16,11 @@ internal sealed class CommandInfo : ICommandContainer
|
|||||||
public IList<string[]> Examples { get; }
|
public IList<string[]> Examples { get; }
|
||||||
|
|
||||||
public bool IsBranch => CommandType == null && Delegate == null;
|
public bool IsBranch => CommandType == null && Delegate == null;
|
||||||
IList<CommandInfo> ICommandContainer.Commands => Children;
|
IList<CommandInfo> ICommandContainer.Commands => Children;
|
||||||
|
|
||||||
// only branches can have a default command
|
// only branches can have a default command
|
||||||
public CommandInfo? DefaultCommand => IsBranch ? Children.FirstOrDefault(c => c.IsDefaultCommand) : null;
|
public CommandInfo? DefaultCommand => IsBranch ? Children.FirstOrDefault(c => c.IsDefaultCommand) : null;
|
||||||
public bool IsHidden { get; }
|
public bool IsHidden { get; }
|
||||||
|
|
||||||
public CommandInfo(CommandInfo? parent, ConfiguredCommand prototype)
|
public CommandInfo(CommandInfo? parent, ConfiguredCommand prototype)
|
||||||
{
|
{
|
||||||
|
@ -1127,6 +1127,37 @@ public sealed partial class CommandAppTests
|
|||||||
// When
|
// When
|
||||||
var result = app.Run(new[] { "foo", "4", "12" });
|
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
|
// Then
|
||||||
result.ShouldBe(1);
|
result.ShouldBe(1);
|
||||||
dog.ShouldNotBeNull();
|
dog.ShouldNotBeNull();
|
||||||
@ -1161,6 +1192,40 @@ public sealed partial class CommandAppTests
|
|||||||
// When
|
// When
|
||||||
var result = app.Run(new[] { "foo", "4", "bar", "12" });
|
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
|
// Then
|
||||||
result.ShouldBe(1);
|
result.ShouldBe(1);
|
||||||
dog.ShouldNotBeNull();
|
dog.ShouldNotBeNull();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user