Пример #1
0
                private static int ErrorHandler(CommandContext?ctx, Exception exception)
                {
                    var errorWriter = (ctx?.Console.Error ?? Console.Error);

                    exception.Print(errorWriter.WriteLine,
                                    includeProperties: true,
                                    includeData: true,
                                    includeStackTrace: false);

                    // use CommandLogger if it has not already logged for this CommandContext
                    if (ctx is not null && !CommandLogger.HasLoggedFor(ctx))
                    {
                        CommandLogger.Log(ctx,
                                          writer: errorWriter.WriteLine,
                                          includeSystemInfo: true,
                                          includeMachineAndUser: true,
                                          includeAppConfig: false
                                          );
                    }

                    // print help for the target command or root command
                    // if the exception occurred before a command could be parsed
                    ctx?.PrintHelp();

                    return(ExitCodes.Error.Result);
                }
Пример #2
0
        private static int Main(string[] args)
        {
            Debugger.AttachIfDebugDirective(args);

            AppDomain.CurrentDomain.UnhandledException += (_, eventArgs)
                                                          => ((Exception)eventArgs.ExceptionObject).Print();

            try
            {
                var configs = AppConfigs.Load();

                var appSettings = new AppSettings
                {
                    Arguments = { DefaultOptionSplit = ',', DefaultPipeTargetSymbol = "$*" },
                    ArgumentTypeDescriptors =
                    {
                        new DelegatedTypeDescriptor <Regex>("regex", p =>
                        {
                            // make it easier to specify NOT matching
                            if (p.StartsWith("$!"))
                            {
                                p = $"^((?!{p.Substring(2)}).)*$";
                            }
                            return(new Regex(p, RegexOptions.Compiled));
                        })
                    }
                };
                var appRunner = new AppRunner <GitApplication>(appSettings)
                                .UseDefaultMiddleware()
                                .GiveCancellationTokenToFlurl()
                                .Configure(c => c.UseParameterResolver(_ => AnsiConsole.Console))
                                .UseNameCasing(Case.KebabCase)
                                .UseDataAnnotationValidations(showHelpOnError: true)
                                .UseTimerDirective()
                                .UseDefaultsFromAppSetting(configs.Settings, true)
                                .UseErrorHandler((ctx, ex) =>
                {
                    var errorWriter = (ctx?.Console.Error ?? Console.Error);
                    ex.Print(errorWriter.WriteLine,
                             includeProperties: true,
                             includeData: true,
                             includeStackTrace: false);

                    // use CommandLogger if it has not already logged for this CommandContext
                    if (ctx is not null && !CommandLogger.HasLoggedFor(ctx))
                    {
                        CommandLogger.Log(ctx,
                                          writer: errorWriter.WriteLine,
                                          includeSystemInfo: true,
                                          includeAppConfig: false
                                          );

                        errorWriter.WriteLine();
                    }

                    // print help for the target command or root command
                    // if the exception occurred before a command could be parsed
                    ctx?.PrintHelp();

                    return(ExitCodes.Error.Result);
                })
                                .UseCommandLogger()
                                .RegisterContainer(configs);

                return(appRunner.Run(args));
            }
            catch (OperationCanceledException)
            {
                return(1);
            }
            catch (Exception e)
            {
                e.Print();
                return(1);
            }
        }