1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Text ;
3
5
using MatthiWare . CommandLine . Abstractions . Command ;
4
6
5
7
namespace MatthiWare . CommandLine . Core . Exceptions
@@ -20,7 +22,36 @@ public class CommandParseException : BaseParserException
20
22
/// <param name="command">the failed command</param>
21
23
/// <param name="innerExceptions">collection of inner exception</param>
22
24
public CommandParseException ( ICommandLineCommand command , IReadOnlyCollection < Exception > innerExceptions )
23
- : base ( command , "" , new AggregateException ( innerExceptions ) )
25
+ : base ( command , CreateMessage ( command , innerExceptions ) , new AggregateException ( innerExceptions ) )
24
26
{ }
27
+
28
+ private static string CreateMessage ( ICommandLineCommand command , IReadOnlyCollection < Exception > exceptions )
29
+ {
30
+ if ( exceptions . Count > 1 )
31
+ {
32
+ return CreateMultipleExceptionsMessage ( command , exceptions ) ;
33
+ }
34
+ else
35
+ {
36
+ return CreateSingleExceptionMessage ( command , exceptions . First ( ) ) ;
37
+ }
38
+ }
39
+
40
+ private static string CreateSingleExceptionMessage ( ICommandLineCommand command , Exception exception )
41
+ => $ "Unable to parse command '{ command . Name } ' because { exception . Message } ";
42
+
43
+
44
+ private static string CreateMultipleExceptionsMessage ( ICommandLineCommand command , IReadOnlyCollection < Exception > exceptions )
45
+ {
46
+ var message = new StringBuilder ( ) ;
47
+ message . AppendLine ( $ "Unable to parse command '{ command . Name } ' because { exceptions . Count } errors occured") ;
48
+
49
+ foreach ( var exception in exceptions )
50
+ {
51
+ message . AppendLine ( $ " - { exception . Message } ") ;
52
+ }
53
+
54
+ return message . ToString ( ) ;
55
+ }
25
56
}
26
57
}
0 commit comments