From 82895f2e4211a886e6df14d9a9130967ab5a9f4a Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Tue, 28 Jan 2020 14:48:21 +0200 Subject: [PATCH] Improve preview directive --- CliFx/CliApplication.cs | 52 +++++++++++++++++---------- CliFx/Domain/ApplicationSchema.cs | 3 +- CliFx/Domain/CommandArgumentSchema.cs | 2 +- CliFx/Domain/CommandOptionSchema.cs | 2 +- CliFx/Domain/CommandSchema.cs | 2 +- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/CliFx/CliApplication.cs b/CliFx/CliApplication.cs index c3a10f3..97c732c 100644 --- a/CliFx/CliApplication.cs +++ b/CliFx/CliApplication.cs @@ -47,35 +47,51 @@ namespace CliFx return null; } - private int? HandlePreviewDirective(CommandLineInput commandLineInput) + private int? HandlePreviewDirective(ApplicationSchema applicationSchema, CommandLineInput commandLineInput) { var isPreviewMode = _configuration.IsPreviewModeAllowed && commandLineInput.IsPreviewDirectiveSpecified; if (!isPreviewMode) return null; - // Render command name - _console.Output.WriteLine($"Arguments: {string.Join(" ", commandLineInput.Arguments)}"); - _console.Output.WriteLine(); + var commandSchema = applicationSchema.TryFindCommand(commandLineInput, out var argumentOffset); - // Render directives - _console.Output.WriteLine("Directives:"); - foreach (var directive in commandLineInput.Directives) + _console.Output.WriteLine("Parser preview:"); + + // Command name + if (commandSchema != null && argumentOffset > 0) { - _console.Output.Write(" "); - _console.Output.WriteLine(directive); + _console.WithForegroundColor(ConsoleColor.Cyan, () => + _console.Output.Write(commandSchema.Name)); + + _console.Output.Write(' '); } - // Margin - _console.Output.WriteLine(); + // Parameters + foreach (var parameter in commandLineInput.Arguments.Skip(argumentOffset)) + { + _console.Output.Write('<'); - // Render options - _console.Output.WriteLine("Options:"); + _console.WithForegroundColor(ConsoleColor.White, () => + _console.Output.Write(parameter)); + + _console.Output.Write('>'); + _console.Output.Write(' '); + } + + // Options foreach (var option in commandLineInput.Options) { - _console.Output.Write(" "); - _console.Output.WriteLine(option); + _console.Output.Write('['); + + _console.WithForegroundColor(ConsoleColor.White, () => + _console.Output.Write(option)); + + _console.Output.Write(']'); + _console.Output.Write(' '); } + _console.Output.WriteLine(); + return 0; } @@ -91,9 +107,7 @@ namespace CliFx return 0; } - private int? HandleHelpOption( - ApplicationSchema applicationSchema, - CommandLineInput commandLineInput) + private int? HandleHelpOption(ApplicationSchema applicationSchema, CommandLineInput commandLineInput) { // Help is rendered either when it's requested or when the user provides no arguments and there is no default command var shouldRenderHelp = @@ -139,7 +153,7 @@ namespace CliFx return await HandleDebugDirectiveAsync(commandLineInput) ?? - HandlePreviewDirective(commandLineInput) ?? + HandlePreviewDirective(applicationSchema, commandLineInput) ?? HandleVersionOption(commandLineInput) ?? HandleHelpOption(applicationSchema, commandLineInput) ?? await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables); diff --git a/CliFx/Domain/ApplicationSchema.cs b/CliFx/Domain/ApplicationSchema.cs index cbd859c..640848d 100644 --- a/CliFx/Domain/ApplicationSchema.cs +++ b/CliFx/Domain/ApplicationSchema.cs @@ -43,7 +43,8 @@ namespace CliFx.Domain ? Commands.Where(c => TryFindParentCommand(c.Name)?.MatchesName(parentCommandName) == true).ToArray() : Commands.Where(c => !string.IsNullOrWhiteSpace(c.Name) && TryFindParentCommand(c.Name) == null).ToArray(); - private CommandSchema? TryFindCommand(CommandLineInput commandLineInput, out int argumentOffset) + // TODO: this out parameter is not a really nice design + public CommandSchema? TryFindCommand(CommandLineInput commandLineInput, out int argumentOffset) { // Try to find the command that contains the most of the input arguments in its name for (var i = commandLineInput.Arguments.Count; i >= 0; i--) diff --git a/CliFx/Domain/CommandArgumentSchema.cs b/CliFx/Domain/CommandArgumentSchema.cs index 49a1b89..02b1a9d 100644 --- a/CliFx/Domain/CommandArgumentSchema.cs +++ b/CliFx/Domain/CommandArgumentSchema.cs @@ -65,7 +65,7 @@ namespace CliFx.Domain private static readonly IFormatProvider ConversionFormatProvider = CultureInfo.InvariantCulture; private static readonly IReadOnlyDictionary> PrimitiveConverters = - new Dictionary> + new Dictionary> { [typeof(object)] = v => v, [typeof(string)] = v => v, diff --git a/CliFx/Domain/CommandOptionSchema.cs b/CliFx/Domain/CommandOptionSchema.cs index 685ce25..3acc2fa 100644 --- a/CliFx/Domain/CommandOptionSchema.cs +++ b/CliFx/Domain/CommandOptionSchema.cs @@ -36,7 +36,7 @@ namespace CliFx.Domain IsRequired = isRequired; } - public bool MatchesName(string name) => + public bool MatchesName(string? name) => !string.IsNullOrWhiteSpace(Name) && string.Equals(Name, name, StringComparison.OrdinalIgnoreCase); diff --git a/CliFx/Domain/CommandSchema.cs b/CliFx/Domain/CommandSchema.cs index d55abaf..b8b6a7e 100644 --- a/CliFx/Domain/CommandSchema.cs +++ b/CliFx/Domain/CommandSchema.cs @@ -38,7 +38,7 @@ namespace CliFx.Domain Parameters = parameters; } - public bool MatchesName(string name) => string.Equals(name, Name, StringComparison.OrdinalIgnoreCase); + public bool MatchesName(string? name) => string.Equals(name, Name, StringComparison.OrdinalIgnoreCase); private void InjectParameters(ICommand command, IReadOnlyList parameterInputs) {