Skip to content

Commit 503e47c

Browse files
FrankRay78patriksvensson
authored andcommitted
Tighten up when to show/hide the application "-v|--version" option.
1 parent 21becc7 commit 503e47c

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/Spectre.Console.Cli/Help/HelpProvider.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,27 @@ public static IReadOnlyList<HelpOption> Get(
8888
new HelpOption("h", "help", null, null, resources.PrintHelpDescription, null),
8989
};
9090

91-
// Version information applies to the entire application
92-
// Include the "-v" option in the help when at the root of the command line application
93-
// Don't allow the "-v" option if users have specified one or more sub-commands
94-
if ((command?.Parent == null) && !(command?.IsBranch ?? false))
91+
// Version information applies to the entire CLI application.
92+
// Whether to show the "-v|--version" option in the help is determined as per:
93+
// - If an application version has been set, and
94+
// -- When at the root of the application, or
95+
// -- When at the root of the application with a default command, unless
96+
// --- The default command has a version option in its settings
97+
if ((command?.Parent == null) && !(command?.IsBranch ?? false) && (command?.IsDefaultCommand ?? true))
9598
{
96-
// Only show the version command if there is an
97-
// application version set.
98-
if (model.ApplicationVersion != null)
99+
// Check whether the default command has a version option in its settings.
100+
var versionCommandOption = command?.Parameters?.OfType<CommandOption>()?.FirstOrDefault(o =>
101+
(o.ShortNames.FirstOrDefault(v => v.Equals("v", StringComparison.OrdinalIgnoreCase)) != null) ||
102+
(o.LongNames.FirstOrDefault(v => v.Equals("version", StringComparison.OrdinalIgnoreCase)) != null));
103+
104+
// Only show the version option if the default command doesn't have a version option in its settings.
105+
if (versionCommandOption == null)
99106
{
100-
parameters.Add(new HelpOption("v", "version", null, null, resources.PrintVersionDescription, null));
107+
// Only show the version option if there is an application version set.
108+
if (model.ApplicationVersion != null)
109+
{
110+
parameters.Add(new HelpOption("v", "version", null, null, resources.PrintVersionDescription, null));
111+
}
101112
}
102113
}
103114

src/Spectre.Console.Cli/Internal/CommandExecutor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public async Task<int> Execute(IConfiguration configuration, IEnumerable<string>
3636
if (firstArgument != null)
3737
{
3838
// Asking for version?
39-
if (firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase) ||
40-
firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase))
39+
if (firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase) ||
40+
firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase))
4141
{
4242
if (configuration.Settings.ApplicationVersion != null)
4343
{

0 commit comments

Comments
 (0)