From cd55898011d9a91978a49b725d2294181d5ce06f Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Tue, 20 Aug 2019 17:24:06 +0300 Subject: [PATCH] Refactor CliApplication --- CliFx/CliApplication.cs | 19 ++++++++++++------- CliFx/Internal/Extensions.cs | 8 ++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CliFx/CliApplication.cs b/CliFx/CliApplication.cs index 2a524cc..75c2158 100644 --- a/CliFx/CliApplication.cs +++ b/CliFx/CliApplication.cs @@ -75,16 +75,21 @@ namespace CliFx // Get parent command schema var parentCommandSchema = availableCommandSchemas.FindParent(commandInput.CommandName); - // Use a stub if parent command schema is not found - if (parentCommandSchema == null) + // Show help for parent command if it's defined + if (parentCommandSchema != null) { - parentCommandSchema = CommandSchema.StubDefaultCommand; - availableCommandSchemas = availableCommandSchemas.Concat(new[] { parentCommandSchema }).ToArray(); + var helpTextSource = new HelpTextSource(_metadata, availableCommandSchemas, parentCommandSchema); + _helpTextRenderer.RenderHelpText(_console, helpTextSource); } + // Otherwise show help for a stub default command + else + { + var helpTextSource = new HelpTextSource(_metadata, + availableCommandSchemas.Concat(CommandSchema.StubDefaultCommand).ToArray(), + CommandSchema.StubDefaultCommand); - // Show help - var helpTextSource = new HelpTextSource(_metadata, availableCommandSchemas, parentCommandSchema); - _helpTextRenderer.RenderHelpText(_console, helpTextSource); + _helpTextRenderer.RenderHelpText(_console, helpTextSource); + } return isError ? -1 : 0; } diff --git a/CliFx/Internal/Extensions.cs b/CliFx/Internal/Extensions.cs index 0e4c65a..7233642 100644 --- a/CliFx/Internal/Extensions.cs +++ b/CliFx/Internal/Extensions.cs @@ -31,6 +31,14 @@ namespace CliFx.Internal public static IEnumerable ExceptNull(this IEnumerable source) where T : class => source.Where(i => i != null); + public static IEnumerable Concat(this IEnumerable source, T value) + { + foreach (var i in source) + yield return i; + + yield return value; + } + public static bool Implements(this Type type, Type interfaceType) => type.GetInterfaces().Contains(interfaceType); public static Type GetEnumerableUnderlyingType(this Type type)