diff --git a/src/System.CommandLine/Help/VersionOption.cs b/src/System.CommandLine/Help/VersionOption.cs index 8e65328ee8..fcf0b53837 100644 --- a/src/System.CommandLine/Help/VersionOption.cs +++ b/src/System.CommandLine/Help/VersionOption.cs @@ -1,8 +1,9 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.CommandLine.Invocation; +using System.CommandLine.IO; using System.CommandLine.Parsing; -using System.Linq; namespace System.CommandLine.Help { @@ -29,23 +30,33 @@ private void AddValidators() { Validators.Add(static result => { - if (result.Parent is CommandResult parent && - parent.Children.Where(r => !(r is OptionResult optionResult && optionResult.Option is VersionOption)) - .Any(IsNotImplicit)) + CommandResult parent = (CommandResult)result.Parent!; + + bool setHandler = true; + foreach (SymbolResult sibling in parent.Children) { - result.AddError(result.LocalizationResources.VersionOptionCannotBeCombinedWithOtherArguments(result.Token?.Value ?? result.Option.Name)); + setHandler = sibling switch + { + OptionResult optionResult => optionResult.IsImplicit || optionResult.Option is VersionOption, + ArgumentResult argumentResult => argumentResult.IsImplicit, + _ => false + }; + + if (!setHandler) + { + result.AddError(result.LocalizationResources.VersionOptionCannotBeCombinedWithOtherArguments(result.Token?.Value ?? result.Option.Name)); + break; + } } - }); - } - private static bool IsNotImplicit(SymbolResult symbolResult) - { - return symbolResult switch - { - ArgumentResult argumentResult => !argumentResult.IsImplicit, - OptionResult optionResult => !optionResult.IsImplicit, - _ => true - }; + if (setHandler) + { + parent.Command.Handler = new AnonymousCommandHandler(static context => + { + context.Console.Out.WriteLine(RootCommand.ExecutableVersion); + }); + } + }); } public override string? Description diff --git a/src/System.CommandLine/Parsing/ParseOperation.cs b/src/System.CommandLine/Parsing/ParseOperation.cs index caf62a54cf..e622d319bd 100644 --- a/src/System.CommandLine/Parsing/ParseOperation.cs +++ b/src/System.CommandLine/Parsing/ParseOperation.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using System.CommandLine.Help; -using System.CommandLine.Invocation; -using System.CommandLine.IO; namespace System.CommandLine.Parsing { @@ -20,7 +18,6 @@ internal sealed class ParseOperation private Dictionary>? _directives; private CommandResult _innermostCommandResult; private bool _isHelpRequested; - private bool _isVersionRequested; public ParseOperation( List tokens, @@ -62,7 +59,7 @@ internal ParseResult Parse(Parser parser) Validate(); } - ParseResult parseResult = new ( + return new ( parser, _rootCommandResult, _innermostCommandResult, @@ -71,17 +68,6 @@ internal ParseResult Parse(Parser parser) _symbolResultTree.UnmatchedTokens, _symbolResultTree.Errors, _rawInput); - - if (_isVersionRequested) - { - // FIX: (GetResult) use the ActiveOption's handler - parseResult.Handler = new AnonymousCommandHandler(context => - { - context.Console.Out.WriteLine(RootCommand.ExecutableVersion); - }); - } - - return parseResult; } private void ParseSubcommand() @@ -191,10 +177,6 @@ private void ParseOption() { _isHelpRequested = true; } - else if (option is VersionOption) - { - _isVersionRequested = true; - } optionResult = new OptionResult( option,