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 duplicate errors appended #2416

Open
wants to merge 1 commit into
base: main-powderhouse
Choose a base branch
from
Open
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
25 changes: 21 additions & 4 deletions src/System.CommandLine.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
using System.Collections.Generic;
using System.CommandLine.Parsing;
using System.CommandLine.Tests.Utility;
using System.IO;
using FluentAssertions;
using FluentAssertions.Equivalency;
using System.Linq;
using FluentAssertions.Common;
using Xunit;
using Xunit.Abstractions;

namespace System.CommandLine.Tests
{
Expand All @@ -19,10 +17,10 @@ public partial class ParserTests
// TODO: Update testing strategy if we use Location in equality. Some will break
private readonly Location dummyLocation = new("", Location.Internal, -1, null);

private T GetValue<T>(ParseResult parseResult, CliOption<T> option)
private static T GetValue<T>(ParseResult parseResult, CliOption<T> option)
=> parseResult.GetValue(option);

private T GetValue<T>(ParseResult parseResult, CliArgument<T> argument)
private static T GetValue<T>(ParseResult parseResult, CliArgument<T> argument)
=> parseResult.GetValue(argument);

//[Fact]
Expand Down Expand Up @@ -1921,5 +1919,24 @@ public void ParseResult_contains_option_ValueResults()
result1.GetValue<string>().Should().Be("Kirk");
result2.GetValue<string>().Should().Be("Spock");
}

// https://github.com/dotnet/command-line-api/issues/2392
[Fact]
public void Getting_argument_value_with_option_argument_error_the_only_a_single_error_is_returned()
{
var option = new CliOption<int[]>("-x") { Arity = new ArgumentArity(2, 3), AllowMultipleArgumentsPerToken = true };
var command = new CliCommand("the-command")
{
option
};

ParseResult parseResult = CliParser.Parse(command, "-x 1 2 3 4");

_ = parseResult.CommandResult.GetValue(option);

parseResult.Errors
.Should()
.ContainSingle();
}
}
}
5 changes: 3 additions & 2 deletions src/System.CommandLine/Parsing/ArgumentResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public ValueResult ValueResult
{
// This is not lazy on the assumption that almost everything the user enters will be used, and ArgumentResult is no longer used for defaults
// TODO: Make sure errors are added
var conversionValue = GetArgumentConversionResult().Value;
ArgumentConversionResult argumentConversionValue = GetArgumentConversionResult();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Mentioning partially because we want to build some guidelines here.

Since the name of the method is GetArgumentConversionResult, I think we should use var.

Thoughts

var conversionValue = argumentConversionValue.Value;
var locations = Tokens.Select(token => token.Location).ToArray();
//TODO: Remove this wrapper later
_valueResult = new ValueResult(Argument, conversionValue, locations, ArgumentResult.GetValueResultOutcome(GetArgumentConversionResult()?.Result)); // null is temporary here
_valueResult = new ValueResult(Argument, conversionValue, locations, GetValueResultOutcome(argumentConversionValue.Result));
}
return _valueResult;
}
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Parsing/OptionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ValueResult ValueResult
var conversionValue = ArgumentConversionResult.Value;
var locations = Tokens.Select(token => token.Location).ToArray();
//TODO: Remove this wrapper later
_valueResult = new ValueResult(Option, conversionValue, locations, ArgumentResult.GetValueResultOutcome(ArgumentConversionResult?.Result)); // null is temporary here
_valueResult = new ValueResult(Option, conversionValue, locations, ArgumentResult.GetValueResultOutcome(ArgumentConversionResult.Result));
}
return _valueResult;
}
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Parsing/SymbolResultTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed class SymbolResultTree : Dictionary<CliSymbol, SymbolResult>
internal List<CliToken>? UnmatchedTokens;
*/

// TODO: Looks like this is a SymboNode/linked list because a symbol may appear multiple
// TODO: Looks like this is a SymbolNode/linked list because a symbol may appear multiple
// places in the tree and multiple symbols will have the same short name. The question is
// whether creating the multiple node instances is faster than just using lists. Could well be.
private Dictionary<string, SymbolNode>? _symbolsByName;
Expand Down