Refactor command type list into ApplicationConfiguration

This commit is contained in:
Alexey Golub
2019-08-17 23:46:55 +03:00
parent 172ec1f15e
commit 7d7edaf30f
3 changed files with 36 additions and 11 deletions

View File

@@ -15,8 +15,8 @@ namespace CliFx
/// </summary> /// </summary>
public partial class CliApplication : ICliApplication public partial class CliApplication : ICliApplication
{ {
private readonly ApplicationMetadata _applicationMetadata; private readonly ApplicationMetadata _metadata;
private readonly IReadOnlyList<Type> _commandTypes; private readonly ApplicationConfiguration _configuration;
private readonly IConsole _console; private readonly IConsole _console;
private readonly ICommandInputParser _commandInputParser; private readonly ICommandInputParser _commandInputParser;
@@ -28,12 +28,12 @@ namespace CliFx
/// <summary> /// <summary>
/// Initializes an instance of <see cref="CliApplication"/>. /// Initializes an instance of <see cref="CliApplication"/>.
/// </summary> /// </summary>
public CliApplication(ApplicationMetadata applicationMetadata, IReadOnlyList<Type> commandTypes, public CliApplication(ApplicationMetadata metadata, ApplicationConfiguration configuration,
IConsole console, ICommandInputParser commandInputParser, ICommandSchemaResolver commandSchemaResolver, IConsole console, ICommandInputParser commandInputParser, ICommandSchemaResolver commandSchemaResolver,
ICommandFactory commandFactory, ICommandInitializer commandInitializer, ICommandHelpTextRenderer commandHelpTextRenderer) ICommandFactory commandFactory, ICommandInitializer commandInitializer, ICommandHelpTextRenderer commandHelpTextRenderer)
{ {
_applicationMetadata = applicationMetadata.GuardNotNull(nameof(applicationMetadata)); _metadata = metadata.GuardNotNull(nameof(metadata));
_commandTypes = commandTypes.GuardNotNull(nameof(commandTypes)); _configuration = configuration.GuardNotNull(nameof(configuration));
_console = console.GuardNotNull(nameof(console)); _console = console.GuardNotNull(nameof(console));
_commandInputParser = commandInputParser.GuardNotNull(nameof(commandInputParser)); _commandInputParser = commandInputParser.GuardNotNull(nameof(commandInputParser));
@@ -131,7 +131,7 @@ namespace CliFx
{ {
var commandInput = _commandInputParser.ParseInput(commandLineArguments); var commandInput = _commandInputParser.ParseInput(commandLineArguments);
var availableCommandSchemas = _commandSchemaResolver.GetCommandSchemas(_commandTypes); var availableCommandSchemas = _commandSchemaResolver.GetCommandSchemas(_configuration.CommandTypes);
var matchingCommandSchema = availableCommandSchemas.FindByName(commandInput.CommandName); var matchingCommandSchema = availableCommandSchemas.FindByName(commandInput.CommandName);
// Validate available command schemas // Validate available command schemas
@@ -169,7 +169,7 @@ namespace CliFx
} }
// Show help // Show help
var helpTextSource = new HelpTextSource(_applicationMetadata, availableCommandSchemas, parentCommandSchema); var helpTextSource = new HelpTextSource(_metadata, availableCommandSchemas, parentCommandSchema);
_commandHelpTextRenderer.RenderHelpText(_console, helpTextSource); _commandHelpTextRenderer.RenderHelpText(_console, helpTextSource);
return isError ? -1 : 0; return isError ? -1 : 0;
@@ -178,7 +178,7 @@ namespace CliFx
// Show version if it was requested without specifying a command // Show version if it was requested without specifying a command
if (commandInput.IsVersionRequested() && !commandInput.IsCommandSpecified()) if (commandInput.IsVersionRequested() && !commandInput.IsCommandSpecified())
{ {
_console.Output.WriteLine(_applicationMetadata.VersionText); _console.Output.WriteLine(_metadata.VersionText);
return 0; return 0;
} }
@@ -186,7 +186,7 @@ namespace CliFx
// Show help if it was requested // Show help if it was requested
if (commandInput.IsHelpRequested()) if (commandInput.IsHelpRequested())
{ {
var helpTextSource = new HelpTextSource(_applicationMetadata, availableCommandSchemas, matchingCommandSchema); var helpTextSource = new HelpTextSource(_metadata, availableCommandSchemas, matchingCommandSchema);
_commandHelpTextRenderer.RenderHelpText(_console, helpTextSource); _commandHelpTextRenderer.RenderHelpText(_console, helpTextSource);
return 0; return 0;

View File

@@ -117,9 +117,9 @@ namespace CliFx
// Project parameters to expected types // Project parameters to expected types
var metadata = new ApplicationMetadata(_title, _executableName, _versionText, _description); 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(), _console, new CommandInputParser(), new CommandSchemaResolver(),
_commandFactory, new CommandInitializer(), new CommandHelpTextRenderer()); _commandFactory, new CommandInitializer(), new CommandHelpTextRenderer());
} }

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using CliFx.Internal;
namespace CliFx.Models
{
/// <summary>
/// Configuration of an application.
/// </summary>
public class ApplicationConfiguration
{
/// <summary>
/// Command types defined in the application.
/// </summary>
public IReadOnlyList<Type> CommandTypes { get; }
/// <summary>
/// Initializes an instance of <see cref="ApplicationConfiguration"/>.
/// </summary>
public ApplicationConfiguration(IReadOnlyList<Type> commandTypes)
{
CommandTypes = commandTypes.GuardNotNull(nameof(commandTypes));
}
}
}