Skip to content

use Validator to set Handler for VersionOption #2042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/System.CommandLine/Help/VersionOption.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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
Expand Down
20 changes: 1 addition & 19 deletions src/System.CommandLine/Parsing/ParseOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System.Collections.Generic;
using System.CommandLine.Help;
using System.CommandLine.Invocation;
using System.CommandLine.IO;

namespace System.CommandLine.Parsing
{
Expand All @@ -20,7 +18,6 @@ internal sealed class ParseOperation
private Dictionary<string, IReadOnlyList<string>>? _directives;
private CommandResult _innermostCommandResult;
private bool _isHelpRequested;
private bool _isVersionRequested;

public ParseOperation(
List<Token> tokens,
Expand Down Expand Up @@ -62,7 +59,7 @@ internal ParseResult Parse(Parser parser)
Validate();
}

ParseResult parseResult = new (
return new (
parser,
_rootCommandResult,
_innermostCommandResult,
Expand All @@ -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()
Expand Down Expand Up @@ -191,10 +177,6 @@ private void ParseOption()
{
_isHelpRequested = true;
}
else if (option is VersionOption)
{
_isVersionRequested = true;
}

optionResult = new OptionResult(
option,
Expand Down