Tighten up when to show/hide the application "-v|--version" option.

This commit is contained in:
Frank Ray 2024-10-15 14:07:10 +01:00 committed by Patrik Svensson
parent c4a97f3c89
commit 958820dd66
2 changed files with 21 additions and 10 deletions

View File

@ -88,18 +88,29 @@ public class HelpProvider : IHelpProvider
new HelpOption("h", "help", null, null, resources.PrintHelpDescription, null), new HelpOption("h", "help", null, null, resources.PrintHelpDescription, null),
}; };
// Version information applies to the entire application // Version information applies to the entire CLI application.
// Include the "-v" option in the help when at the root of the command line application // Whether to show the "-v|--version" option in the help is determined as per:
// Don't allow the "-v" option if users have specified one or more sub-commands // - If an application version has been set, and
if ((command?.Parent == null) && !(command?.IsBranch ?? false)) // -- 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 // Check whether the default command has a version option in its settings.
// application version set. var versionCommandOption = command?.Parameters?.OfType<CommandOption>()?.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)
{
// Only show the version option if there is an application version set.
if (model.ApplicationVersion != null) if (model.ApplicationVersion != null)
{ {
parameters.Add(new HelpOption("v", "version", null, null, resources.PrintVersionDescription, null)); parameters.Add(new HelpOption("v", "version", null, null, resources.PrintVersionDescription, null));
} }
} }
}
parameters.AddRange(command?.Parameters.OfType<ICommandOption>().Where(o => !o.IsHidden).Select(o => parameters.AddRange(command?.Parameters.OfType<ICommandOption>().Where(o => !o.IsHidden).Select(o =>
new HelpOption( new HelpOption(

View File

@ -36,8 +36,8 @@ internal sealed class CommandExecutor
if (firstArgument != null) if (firstArgument != null)
{ {
// Asking for version? // Asking for version?
if (firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase) || if (firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase) ||
firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase)) firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase))
{ {
if (configuration.Settings.ApplicationVersion != null) if (configuration.Settings.ApplicationVersion != null)
{ {