Skip to content
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

Fix version option validation #1270

Merged
merged 7 commits into from
May 7, 2021
Merged

Conversation

JoeRobich
Copy link
Member

@JoeRobich JoeRobich commented May 6, 2021

As reported at dotnet/format#1136

The version option validation (introduced in #1124) treats default options and arguments as if they were passed by the user. This PR adds an IsImplicit property to ArgumentResult and filters implicit argument and option results when validating the version option.

@@ -20,9 +20,11 @@ public class ArgumentResult : SymbolResult

public IArgument Argument { get; }

public bool IsImplicit => PassedOnTokens == null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is correct. IsImplicit means something a bit different in the existing OptionResult.IsImplicit, which is that the token was generated by the parser as a sort of placeholder for a default value.

At the least, the PassedOnTokens == null case looks like it might only accidentally correlate with the presence of a default value. This collection is intended to capture the overflow when a custom parser doesn't want to consume all of the available tokens.

@@ -526,7 +526,7 @@ public static CommandLineBuilder UseHelp(this CommandLineBuilder builder)
description: Resources.Instance.VersionOptionDescription(),
parseArgument: result =>
{
if (result.FindResultFor(command)?.Children.Count > 1)
if (result.FindResultFor(command)?.Children.Where(IsNotImplicit).Count() > 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this without the LINQ? A bunch of LINQ removal was done for performance reasons.

I wonder if a simpler implementation could work based on just the token count?

var versionOptionResult = result.Parent;
foreach (var symbolResult in result.FindResultFor(command)?.Children)
foreach (var symbolResult in commandChildren)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use a for loop here.

@@ -526,8 +526,14 @@ public static CommandLineBuilder UseHelp(this CommandLineBuilder builder)
description: Resources.Instance.VersionOptionDescription(),
parseArgument: result =>
{
var commandChildren = result.FindResultFor(command)?.Children;
if (commandChildren == null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (commandChildren == null)
if (commandChildren is null)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants