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()
864
864
var value = option . Parse ( "-x Notaday" ) ;
865
865
866
866
value . Errors
867
- . Select ( e => e . Message )
867
+ . Should ( )
868
+ . ContainSingle ( )
869
+ . Which
870
+ . Message
868
871
. Should ( )
869
872
. Contain ( "Cannot parse argument 'Notaday' for option '-x' as expected type 'System.DayOfWeek'." ) ;
870
873
}
Original file line number Diff line number Diff line change 1
1
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
- using System . Collections . Generic ;
5
4
using System . CommandLine . Builder ;
6
5
using System . CommandLine . Parsing ;
7
6
using System . CommandLine . Tests . Utility ;
10
9
using FluentAssertions ;
11
10
using Xunit ;
12
11
using Xunit . Abstractions ;
12
+ using static System . Environment ;
13
13
14
14
namespace System . CommandLine . Tests
15
15
{
@@ -974,5 +974,22 @@ public void Completions_for_subcommands_provide_a_description()
974
974
. Should ( )
975
975
. Be ( description ) ;
976
976
}
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
+ }
977
994
}
978
995
}
Original file line number Diff line number Diff line change @@ -483,22 +483,31 @@ private void ValidateAndConvertOptionResult(OptionResult optionResult)
483
483
484
484
private void ValidateAndConvertArgumentResult ( ArgumentResult argumentResult )
485
485
{
486
- if ( argumentResult . Argument is { } argument )
487
- {
488
- var parseError =
489
- argumentResult . Parent ? . UnrecognizedArgumentError ( argument ) ??
490
- argumentResult . CustomError ( argument ) ;
486
+ var argument = argumentResult . Argument ;
491
487
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 ;
497
496
}
498
497
499
- if ( argumentResult . GetArgumentConversionResult ( ) is FailedArgumentConversionResult failed
498
+ if ( argumentResult . GetArgumentConversionResult ( ) is FailedArgumentConversionResult failed
500
499
and not FailedArgumentConversionArityResult )
501
500
{
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
+
502
511
AddErrorToResult ( argumentResult , new ParseError ( failed . ErrorMessage ! , argumentResult ) ) ;
503
512
}
504
513
}
You can’t perform that action at this time.
0 commit comments