diff --git a/src/System.CommandLine.Tests/CompletionTests.cs b/src/System.CommandLine.Tests/CompletionTests.cs index 67e59ea177..705717cd68 100644 --- a/src/System.CommandLine.Tests/CompletionTests.cs +++ b/src/System.CommandLine.Tests/CompletionTests.cs @@ -888,7 +888,25 @@ public void Default_completions_can_be_cleared_and_replaced() } [Fact] - public void Default_completions_can_be_appended_to() + public void Default_completions_are_not_used_when_user_provides_their_own() + { + var argument = new Argument(); + argument.Completions.Add(new[] { "mon", "tues", "wed", "thur", "fri", "sat", "sun" }); + var command = new Command("the-command") + { + argument + }; + + var completions = command.Parse("the-command s") + .GetCompletions(); + + completions.Select(item => item.Label) + .Should() + .BeEquivalentTo("sat", "sun", "tues"); + } + + [Fact] + public void Default_completions_can_not_be_appended_to() { var command = new Command("the-command") { @@ -906,13 +924,8 @@ public void Default_completions_can_be_appended_to() .Should() .BeEquivalentTo( "sat", - nameof(DayOfWeek.Saturday), "sun", - nameof(DayOfWeek.Sunday), - "tues", - nameof(DayOfWeek.Tuesday), - nameof(DayOfWeek.Thursday), - nameof(DayOfWeek.Wednesday)); + "tues"); } [Fact] diff --git a/src/System.CommandLine/Argument.cs b/src/System.CommandLine/Argument.cs index e6300bf1bc..9903ea69b8 100644 --- a/src/System.CommandLine/Argument.cs +++ b/src/System.CommandLine/Argument.cs @@ -74,11 +74,7 @@ internal TryConvertArgument? ConvertArguments /// /// Gets the collection of completion sources for the argument. /// - public ICollection Completions => - _completions ??= new () - { - CompletionSource.ForType(ValueType) - }; + public ICollection Completions => _completions ??= new(); /// /// Gets or sets the that the argument token(s) will be converted to. @@ -191,7 +187,12 @@ internal void AddAllowedValues(IReadOnlyList values) /// public override IEnumerable GetCompletions(CompletionContext context) { - return Completions + var completions = _completions ?? new() + { + CompletionSource.ForType(ValueType) + }; + + return completions .SelectMany(source => source.GetCompletions(context)) .Distinct() .OrderBy(c => c.SortText, StringComparer.OrdinalIgnoreCase);