@@ -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
0 commit comments