mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-07-05 03:58:16 +08:00
Add the possibility to register multiple interceptors (#1412)
Having the interceptors registered with the ITypeRegistrar also enables the usage of ITypeResolver in interceptors.
This commit is contained in:
@ -121,7 +121,7 @@ internal sealed class CommandExecutor
|
||||
VersionHelper.GetVersion(Assembly.GetEntryAssembly());
|
||||
}
|
||||
|
||||
private static Task<int> Execute(
|
||||
private static async Task<int> Execute(
|
||||
CommandTree leaf,
|
||||
CommandTree tree,
|
||||
CommandContext context,
|
||||
@ -130,7 +130,19 @@ internal sealed class CommandExecutor
|
||||
{
|
||||
// Bind the command tree against the settings.
|
||||
var settings = CommandBinder.Bind(tree, leaf.Command.SettingsType, resolver);
|
||||
configuration.Settings.Interceptor?.Intercept(context, settings);
|
||||
var interceptors =
|
||||
((IEnumerable<ICommandInterceptor>?)resolver.Resolve(typeof(IEnumerable<ICommandInterceptor>))
|
||||
?? Array.Empty<ICommandInterceptor>()).ToList();
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
if (configuration.Settings.Interceptor != null)
|
||||
{
|
||||
interceptors.Add(configuration.Settings.Interceptor);
|
||||
}
|
||||
#pragma warning restore CS0618 // Type or member is obsolete
|
||||
foreach (var interceptor in interceptors)
|
||||
{
|
||||
interceptor.Intercept(context, settings);
|
||||
}
|
||||
|
||||
// Create and validate the command.
|
||||
var command = leaf.CreateCommand(resolver);
|
||||
@ -141,6 +153,12 @@ internal sealed class CommandExecutor
|
||||
}
|
||||
|
||||
// Execute the command.
|
||||
return command.Execute(context, settings);
|
||||
var result = await command.Execute(context, settings);
|
||||
foreach (var interceptor in interceptors)
|
||||
{
|
||||
interceptor.InterceptResult(context, settings, ref result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ internal sealed class CommandAppSettings : ICommandAppSettings
|
||||
public int MaximumIndirectExamples { get; set; }
|
||||
public bool ShowOptionDefaultValues { get; set; }
|
||||
public IAnsiConsole? Console { get; set; }
|
||||
[Obsolete("Register the interceptor with the ITypeRegistrar.")]
|
||||
public ICommandInterceptor? Interceptor { get; set; }
|
||||
public ITypeRegistrarFrontend Registrar { get; set; }
|
||||
public CaseSensitivity CaseSensitivity { get; set; }
|
||||
|
Reference in New Issue
Block a user