From 958820dd669cab481fdb54495272b06f3dee46c9 Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:07:10 +0100 Subject: [PATCH] Tighten up when to show/hide the application "-v|--version" option. --- src/Spectre.Console.Cli/Help/HelpProvider.cs | 27 +++++++++++++------ .../Internal/CommandExecutor.cs | 4 +-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Spectre.Console.Cli/Help/HelpProvider.cs b/src/Spectre.Console.Cli/Help/HelpProvider.cs index eb27ee2..f1c7daa 100644 --- a/src/Spectre.Console.Cli/Help/HelpProvider.cs +++ b/src/Spectre.Console.Cli/Help/HelpProvider.cs @@ -88,16 +88,27 @@ public class HelpProvider : IHelpProvider new HelpOption("h", "help", null, null, resources.PrintHelpDescription, null), }; - // Version information applies to the entire application - // Include the "-v" option in the help when at the root of the command line application - // Don't allow the "-v" option if users have specified one or more sub-commands - if ((command?.Parent == null) && !(command?.IsBranch ?? false)) + // Version information applies to the entire CLI application. + // Whether to show the "-v|--version" option in the help is determined as per: + // - If an application version has been set, and + // -- When at the root of the application, or + // -- When at the root of the application with a default command, unless + // --- The default command has a version option in its settings + if ((command?.Parent == null) && !(command?.IsBranch ?? false) && (command?.IsDefaultCommand ?? true)) { - // Only show the version command if there is an - // application version set. - if (model.ApplicationVersion != null) + // Check whether the default command has a version option in its settings. + var versionCommandOption = command?.Parameters?.OfType()?.FirstOrDefault(o => + (o.ShortNames.FirstOrDefault(v => v.Equals("v", StringComparison.OrdinalIgnoreCase)) != null) || + (o.LongNames.FirstOrDefault(v => v.Equals("version", StringComparison.OrdinalIgnoreCase)) != null)); + + // Only show the version option if the default command doesn't have a version option in its settings. + if (versionCommandOption == null) { - parameters.Add(new HelpOption("v", "version", null, null, resources.PrintVersionDescription, null)); + // Only show the version option if there is an application version set. + if (model.ApplicationVersion != null) + { + parameters.Add(new HelpOption("v", "version", null, null, resources.PrintVersionDescription, null)); + } } } diff --git a/src/Spectre.Console.Cli/Internal/CommandExecutor.cs b/src/Spectre.Console.Cli/Internal/CommandExecutor.cs index 2032936..be599bc 100644 --- a/src/Spectre.Console.Cli/Internal/CommandExecutor.cs +++ b/src/Spectre.Console.Cli/Internal/CommandExecutor.cs @@ -36,8 +36,8 @@ internal sealed class CommandExecutor if (firstArgument != null) { // Asking for version? - if (firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase) || - firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase)) + if (firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase) || + firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase)) { if (configuration.Settings.ApplicationVersion != null) {