Skip to content

Commit 8152e35

Browse files
committed
fix #1629
1 parent 7b3d683 commit 8152e35

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

src/System.CommandLine.Tests/Binding/TypeConversionTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,10 @@ public void Enum_values_that_cannot_be_parsed_result_in_an_informative_error()
864864
var value = option.Parse("-x Notaday");
865865

866866
value.Errors
867-
.Select(e => e.Message)
867+
.Should()
868+
.ContainSingle()
869+
.Which
870+
.Message
868871
.Should()
869872
.Contain("Cannot parse argument 'Notaday' for option '-x' as expected type 'System.DayOfWeek'.");
870873
}

src/System.CommandLine.Tests/CompletionTests.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.Collections.Generic;
54
using System.CommandLine.Builder;
65
using System.CommandLine.Parsing;
76
using System.CommandLine.Tests.Utility;
@@ -10,6 +9,7 @@
109
using FluentAssertions;
1110
using Xunit;
1211
using Xunit.Abstractions;
12+
using static System.Environment;
1313

1414
namespace System.CommandLine.Tests
1515
{
@@ -974,5 +974,22 @@ public void Completions_for_subcommands_provide_a_description()
974974
.Should()
975975
.Be(description);
976976
}
977+
978+
[Fact] // https://github.com/dotnet/command-line-api/issues/1629
979+
public void When_option_completions_are_available_then_they_are_suggested_when_a_validation_error_occurs()
980+
{
981+
var option = new Option<DayOfWeek>("--day");
982+
983+
var result = option.Parse("--day SleepyDay");
984+
985+
result.Errors
986+
.Should()
987+
.ContainSingle()
988+
.Which
989+
.Message
990+
.Should()
991+
.Be(
992+
$"Cannot parse argument 'SleepyDay' for option '--day' as expected type 'System.DayOfWeek'. Did you mean one of the following?{NewLine}Friday{NewLine}Monday{NewLine}Saturday{NewLine}Sunday{NewLine}Thursday{NewLine}Tuesday{NewLine}Wednesday");
993+
}
977994
}
978995
}

src/System.CommandLine/Parsing/ParseResultVisitor.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -483,22 +483,31 @@ private void ValidateAndConvertOptionResult(OptionResult optionResult)
483483

484484
private void ValidateAndConvertArgumentResult(ArgumentResult argumentResult)
485485
{
486-
if (argumentResult.Argument is { } argument)
487-
{
488-
var parseError =
489-
argumentResult.Parent?.UnrecognizedArgumentError(argument) ??
490-
argumentResult.CustomError(argument);
486+
var argument = argumentResult.Argument;
491487

492-
if (parseError is { })
493-
{
494-
AddErrorToResult(argumentResult, parseError);
495-
return;
496-
}
488+
var parseError =
489+
argumentResult.Parent?.UnrecognizedArgumentError(argument) ??
490+
argumentResult.CustomError(argument);
491+
492+
if (parseError is { })
493+
{
494+
AddErrorToResult(argumentResult, parseError);
495+
return;
497496
}
498497

499-
if (argumentResult.GetArgumentConversionResult() is FailedArgumentConversionResult failed
498+
if (argumentResult.GetArgumentConversionResult() is FailedArgumentConversionResult failed
500499
and not FailedArgumentConversionArityResult)
501500
{
501+
if (argument.Parents.FirstOrDefault() is Option option)
502+
{
503+
var completions = option.GetCompletions().ToArray();
504+
505+
if (completions.Length > 0)
506+
{
507+
failed.ErrorMessage += " Did you mean one of the following?" + Environment.NewLine + string.Join(Environment.NewLine, completions.Select(c => c.Label));
508+
}
509+
}
510+
502511
AddErrorToResult(argumentResult, new ParseError(failed.ErrorMessage!, argumentResult));
503512
}
504513
}

0 commit comments

Comments
 (0)