From 7d7edaf30fd0e530b9d43b24f3ffd3bbbe0b658f Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sat, 17 Aug 2019 23:46:55 +0300 Subject: [PATCH] Refactor command type list into ApplicationConfiguration --- CliFx/CliApplication.cs | 18 ++++++++--------- CliFx/CliApplicationBuilder.cs | 4 ++-- CliFx/Models/ApplicationConfiguration.cs | 25 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 CliFx/Models/ApplicationConfiguration.cs diff --git a/CliFx/CliApplication.cs b/CliFx/CliApplication.cs index 91d98a2..0d3a7ad 100644 --- a/CliFx/CliApplication.cs +++ b/CliFx/CliApplication.cs @@ -15,8 +15,8 @@ namespace CliFx /// public partial class CliApplication : ICliApplication { - private readonly ApplicationMetadata _applicationMetadata; - private readonly IReadOnlyList _commandTypes; + private readonly ApplicationMetadata _metadata; + private readonly ApplicationConfiguration _configuration; private readonly IConsole _console; private readonly ICommandInputParser _commandInputParser; @@ -28,12 +28,12 @@ namespace CliFx /// /// Initializes an instance of . /// - public CliApplication(ApplicationMetadata applicationMetadata, IReadOnlyList commandTypes, + public CliApplication(ApplicationMetadata metadata, ApplicationConfiguration configuration, IConsole console, ICommandInputParser commandInputParser, ICommandSchemaResolver commandSchemaResolver, ICommandFactory commandFactory, ICommandInitializer commandInitializer, ICommandHelpTextRenderer commandHelpTextRenderer) { - _applicationMetadata = applicationMetadata.GuardNotNull(nameof(applicationMetadata)); - _commandTypes = commandTypes.GuardNotNull(nameof(commandTypes)); + _metadata = metadata.GuardNotNull(nameof(metadata)); + _configuration = configuration.GuardNotNull(nameof(configuration)); _console = console.GuardNotNull(nameof(console)); _commandInputParser = commandInputParser.GuardNotNull(nameof(commandInputParser)); @@ -131,7 +131,7 @@ namespace CliFx { var commandInput = _commandInputParser.ParseInput(commandLineArguments); - var availableCommandSchemas = _commandSchemaResolver.GetCommandSchemas(_commandTypes); + var availableCommandSchemas = _commandSchemaResolver.GetCommandSchemas(_configuration.CommandTypes); var matchingCommandSchema = availableCommandSchemas.FindByName(commandInput.CommandName); // Validate available command schemas @@ -169,7 +169,7 @@ namespace CliFx } // Show help - var helpTextSource = new HelpTextSource(_applicationMetadata, availableCommandSchemas, parentCommandSchema); + var helpTextSource = new HelpTextSource(_metadata, availableCommandSchemas, parentCommandSchema); _commandHelpTextRenderer.RenderHelpText(_console, helpTextSource); return isError ? -1 : 0; @@ -178,7 +178,7 @@ namespace CliFx // Show version if it was requested without specifying a command if (commandInput.IsVersionRequested() && !commandInput.IsCommandSpecified()) { - _console.Output.WriteLine(_applicationMetadata.VersionText); + _console.Output.WriteLine(_metadata.VersionText); return 0; } @@ -186,7 +186,7 @@ namespace CliFx // Show help if it was requested if (commandInput.IsHelpRequested()) { - var helpTextSource = new HelpTextSource(_applicationMetadata, availableCommandSchemas, matchingCommandSchema); + var helpTextSource = new HelpTextSource(_metadata, availableCommandSchemas, matchingCommandSchema); _commandHelpTextRenderer.RenderHelpText(_console, helpTextSource); return 0; diff --git a/CliFx/CliApplicationBuilder.cs b/CliFx/CliApplicationBuilder.cs index 57c2ac7..270d6b3 100644 --- a/CliFx/CliApplicationBuilder.cs +++ b/CliFx/CliApplicationBuilder.cs @@ -117,9 +117,9 @@ namespace CliFx // Project parameters to expected types var metadata = new ApplicationMetadata(_title, _executableName, _versionText, _description); - var commandTypes = _commandTypes.ToArray(); + var configuration = new ApplicationConfiguration(_commandTypes.ToArray()); - return new CliApplication(metadata, commandTypes, + return new CliApplication(metadata, configuration, _console, new CommandInputParser(), new CommandSchemaResolver(), _commandFactory, new CommandInitializer(), new CommandHelpTextRenderer()); } diff --git a/CliFx/Models/ApplicationConfiguration.cs b/CliFx/Models/ApplicationConfiguration.cs new file mode 100644 index 0000000..f59f1ec --- /dev/null +++ b/CliFx/Models/ApplicationConfiguration.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using CliFx.Internal; + +namespace CliFx.Models +{ + /// + /// Configuration of an application. + /// + public class ApplicationConfiguration + { + /// + /// Command types defined in the application. + /// + public IReadOnlyList CommandTypes { get; } + + /// + /// Initializes an instance of . + /// + public ApplicationConfiguration(IReadOnlyList commandTypes) + { + CommandTypes = commandTypes.GuardNotNull(nameof(commandTypes)); + } + } +} \ No newline at end of file