(#1313) removed the default registration of the IHelpProvider

This commit is contained in:
Nils Andresen 2023-11-10 21:04:20 +01:00 committed by Patrik Svensson
parent 4219bbbf61
commit 1fd028942f

View File

@ -1,20 +1,20 @@
namespace Spectre.Console.Cli; namespace Spectre.Console.Cli;
internal sealed class CommandExecutor internal sealed class CommandExecutor
{ {
private readonly ITypeRegistrar _registrar; private readonly ITypeRegistrar _registrar;
public CommandExecutor(ITypeRegistrar registrar) public CommandExecutor(ITypeRegistrar registrar)
{ {
_registrar = registrar ?? throw new ArgumentNullException(nameof(registrar)); _registrar = registrar ?? throw new ArgumentNullException(nameof(registrar));
_registrar.Register(typeof(DefaultPairDeconstructor), typeof(DefaultPairDeconstructor)); _registrar.Register(typeof(DefaultPairDeconstructor), typeof(DefaultPairDeconstructor));
} }
public async Task<int> Execute(IConfiguration configuration, IEnumerable<string> args) public async Task<int> Execute(IConfiguration configuration, IEnumerable<string> args)
{ {
if (configuration == null) if (configuration == null)
{ {
throw new ArgumentNullException(nameof(configuration)); throw new ArgumentNullException(nameof(configuration));
} }
args ??= new List<string>(); args ??= new List<string>();
@ -22,13 +22,9 @@ internal sealed class CommandExecutor
_registrar.RegisterInstance(typeof(IConfiguration), configuration); _registrar.RegisterInstance(typeof(IConfiguration), configuration);
_registrar.RegisterLazy(typeof(IAnsiConsole), () => configuration.Settings.Console.GetConsole()); _registrar.RegisterLazy(typeof(IAnsiConsole), () => configuration.Settings.Console.GetConsole());
// Register the help provider // Create the command model.
var defaultHelpProvider = new HelpProvider(configuration.Settings); var model = CommandModelBuilder.Build(configuration);
_registrar.RegisterInstance(typeof(IHelpProvider), defaultHelpProvider); _registrar.RegisterInstance(typeof(CommandModel), model);
// Create the command model.
var model = CommandModelBuilder.Build(configuration);
_registrar.RegisterInstance(typeof(CommandModel), model);
_registrar.RegisterDependencies(model); _registrar.RegisterDependencies(model);
// Asking for version? Kind of a hack, but it's alright. // Asking for version? Kind of a hack, but it's alright.
@ -39,20 +35,21 @@ internal sealed class CommandExecutor
console.WriteLine(ResolveApplicationVersion(configuration)); console.WriteLine(ResolveApplicationVersion(configuration));
return 0; return 0;
} }
// Parse and map the model against the arguments. // Parse and map the model against the arguments.
var parsedResult = ParseCommandLineArguments(model, configuration.Settings, args); var parsedResult = ParseCommandLineArguments(model, configuration.Settings, args);
// Register the arguments with the container. // Register the arguments with the container.
_registrar.RegisterInstance(typeof(CommandTreeParserResult), parsedResult); _registrar.RegisterInstance(typeof(CommandTreeParserResult), parsedResult);
_registrar.RegisterInstance(typeof(IRemainingArguments), parsedResult.Remaining); _registrar.RegisterInstance(typeof(IRemainingArguments), parsedResult.Remaining);
// Create the resolver. // Create the resolver.
using (var resolver = new TypeResolverAdapter(_registrar.Build())) using (var resolver = new TypeResolverAdapter(_registrar.Build()))
{ {
// Get the registered help provider, falling back to the default provider // Get the registered help provider, falling back to the default provider
// registered above if no custom implementations have been registered. // if no custom implementations have been registered.
var helpProvider = resolver.Resolve(typeof(IHelpProvider)) as IHelpProvider ?? defaultHelpProvider; var helpProviders = resolver.Resolve(typeof(IEnumerable<IHelpProvider>)) as IEnumerable<IHelpProvider>;
var helpProvider = helpProviders?.LastOrDefault() ?? new HelpProvider(configuration.Settings);
// Currently the root? // Currently the root?
if (parsedResult?.Tree == null) if (parsedResult?.Tree == null)
@ -77,14 +74,14 @@ internal sealed class CommandExecutor
// Display help for default command. // Display help for default command.
configuration.Settings.Console.SafeRender(helpProvider.Write(model, leaf.Command)); configuration.Settings.Console.SafeRender(helpProvider.Write(model, leaf.Command));
return 1; return 1;
} }
// Create the content. // Create the content.
var context = new CommandContext(parsedResult.Remaining, leaf.Command.Name, leaf.Command.Data); var context = new CommandContext(parsedResult.Remaining, leaf.Command.Name, leaf.Command.Data);
// Execute the command tree. // Execute the command tree.
return await Execute(leaf, parsedResult.Tree, context, resolver, configuration).ConfigureAwait(false); return await Execute(leaf, parsedResult.Tree, context, resolver, configuration).ConfigureAwait(false);
} }
} }
#pragma warning disable CS8603 // Possible null reference return. #pragma warning disable CS8603 // Possible null reference return.
@ -100,7 +97,7 @@ internal sealed class CommandExecutor
var lastParsedCommand = lastParsedLeaf?.Command; var lastParsedCommand = lastParsedLeaf?.Command;
if (lastParsedLeaf != null && lastParsedCommand != null && if (lastParsedLeaf != null && lastParsedCommand != null &&
lastParsedCommand.IsBranch && !lastParsedLeaf.ShowHelp && lastParsedCommand.IsBranch && !lastParsedLeaf.ShowHelp &&
lastParsedCommand.DefaultCommand != null) lastParsedCommand.DefaultCommand != null)
{ {
// Insert this branch's default command into the command line // Insert this branch's default command into the command line
// arguments and try again to see if it will parse. // arguments and try again to see if it will parse.
@ -117,33 +114,33 @@ internal sealed class CommandExecutor
} }
#pragma warning restore CS8603 // Possible null reference return. #pragma warning restore CS8603 // Possible null reference return.
private static string ResolveApplicationVersion(IConfiguration configuration) private static string ResolveApplicationVersion(IConfiguration configuration)
{ {
return return
configuration.Settings.ApplicationVersion ?? // potential override configuration.Settings.ApplicationVersion ?? // potential override
VersionHelper.GetVersion(Assembly.GetEntryAssembly()); VersionHelper.GetVersion(Assembly.GetEntryAssembly());
} }
private static Task<int> Execute( private static Task<int> Execute(
CommandTree leaf, CommandTree leaf,
CommandTree tree, CommandTree tree,
CommandContext context, CommandContext context,
ITypeResolver resolver, ITypeResolver resolver,
IConfiguration configuration) IConfiguration configuration)
{ {
// Bind the command tree against the settings. // Bind the command tree against the settings.
var settings = CommandBinder.Bind(tree, leaf.Command.SettingsType, resolver); var settings = CommandBinder.Bind(tree, leaf.Command.SettingsType, resolver);
configuration.Settings.Interceptor?.Intercept(context, settings); configuration.Settings.Interceptor?.Intercept(context, settings);
// Create and validate the command. // Create and validate the command.
var command = leaf.CreateCommand(resolver); var command = leaf.CreateCommand(resolver);
var validationResult = command.Validate(context, settings); var validationResult = command.Validate(context, settings);
if (!validationResult.Successful) if (!validationResult.Successful)
{ {
throw CommandRuntimeException.ValidationFailed(validationResult); throw CommandRuntimeException.ValidationFailed(validationResult);
} }
// Execute the command. // Execute the command.
return command.Execute(context, settings); return command.Execute(context, settings);
} }
} }