IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
CommandLineBuilderExtensions.cs
Go to the documentation of this file.
1using System;
2using System.CommandLine.Builder;
3using System.CommandLine.Invocation;
4using System.CommandLine.Rendering;
5using System.Runtime.ExceptionServices;
6
8{
9
11 {
12
13 public static CommandLineBuilder UseToolParseError(this CommandLineBuilder builder, int? errorExitCode = null)
14 {
15 return builder.AddMiddleware(OnParseErrorReporting(errorExitCode), MiddlewareOrder.ErrorReporting);
16 }
17
18 static InvocationMiddleware OnParseErrorReporting(int? errorExitCode)
19 {
20 return async (context, next) =>
21 {
22 if (context.ParseResult.Errors.Count > 0)
23 context.InvocationResult = new ParseErrorResult(errorExitCode);
24 else
25 await next(context);
26 };
27 }
28
29 public static CommandLineBuilder UseToolErrorExceptionHandler(this CommandLineBuilder builder)
30 {
31 return builder.UseExceptionHandler(OnException, 1);
32 }
33
34 static void OnException(Exception exception, InvocationContext context)
35 {
36 if (exception is ToolErrorException e)
37 {
38 var terminal = Terminal.GetTerminal(context.Console);
39 terminal.Render(new ContainerSpan(ForegroundColorSpan.Red(), new ContentSpan(e.Message), ForegroundColorSpan.Reset()));
40 return;
41 }
42
43 // rethrow exception
44 ExceptionDispatchInfo.Capture(exception).Throw();
45 }
46
47 }
48
49}
static CommandLineBuilder UseToolParseError(this CommandLineBuilder builder, int? errorExitCode=null)
static CommandLineBuilder UseToolErrorExceptionHandler(this CommandLineBuilder builder)