Refactor CliApplication

This commit is contained in:
Alexey Golub
2019-08-20 17:24:06 +03:00
parent 272c079767
commit cd55898011
2 changed files with 20 additions and 7 deletions

View File

@@ -75,16 +75,21 @@ namespace CliFx
// Get parent command schema // Get parent command schema
var parentCommandSchema = availableCommandSchemas.FindParent(commandInput.CommandName); var parentCommandSchema = availableCommandSchemas.FindParent(commandInput.CommandName);
// Use a stub if parent command schema is not found // Show help for parent command if it's defined
if (parentCommandSchema == null) if (parentCommandSchema != null)
{ {
parentCommandSchema = CommandSchema.StubDefaultCommand; var helpTextSource = new HelpTextSource(_metadata, availableCommandSchemas, parentCommandSchema);
availableCommandSchemas = availableCommandSchemas.Concat(new[] { parentCommandSchema }).ToArray(); _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 _helpTextRenderer.RenderHelpText(_console, helpTextSource);
var helpTextSource = new HelpTextSource(_metadata, availableCommandSchemas, parentCommandSchema); }
_helpTextRenderer.RenderHelpText(_console, helpTextSource);
return isError ? -1 : 0; return isError ? -1 : 0;
} }

View File

@@ -31,6 +31,14 @@ namespace CliFx.Internal
public static IEnumerable<T> ExceptNull<T>(this IEnumerable<T> source) where T : class => source.Where(i => i != null); public static IEnumerable<T> ExceptNull<T>(this IEnumerable<T> source) where T : class => source.Where(i => i != null);
public static IEnumerable<T> Concat<T>(this IEnumerable<T> 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 bool Implements(this Type type, Type interfaceType) => type.GetInterfaces().Contains(interfaceType);
public static Type GetEnumerableUnderlyingType(this Type type) public static Type GetEnumerableUnderlyingType(this Type type)