File tree Expand file tree Collapse file tree 3 files changed +42
-13
lines changed
System.CommandLine/Parsing Expand file tree Collapse file tree 3 files changed +42
-13
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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 ;
54using System . CommandLine . Builder ;
65using System . CommandLine . Parsing ;
76using System . CommandLine . Tests . Utility ;
109using FluentAssertions ;
1110using Xunit ;
1211using Xunit . Abstractions ;
12+ using static System . Environment ;
1313
1414namespace 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}
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments