Ejemplo n.º 1
0
        public int Run(string[] args)
        {
            // create a console window for this application (or assign it to the existing console window)
            if (!ConsoleUtil.AttachConsole(-1))
            {
                ConsoleUtil.AllocConsole();
            }

            // set up special console logging for headless mode
            using (var log = new LoggerConfiguration()
                             .MinimumLevel.ControlledBy(LoggingUtil.LoggingLevelSwitch)
                             .WriteTo.Logger(Log.Logger)
                             .WriteTo.Console(outputTemplate: HEADLESS_LOG_FORMAT)
                             .CreateLogger())
            {
                Log.Logger = log;

                // parse the arguments, and log any errors
                int exitCode = 0;
                try
                {
                    var commandTypes = Commands.Select(c => c.OptionsType).ToArray();
                    var parseResult  = Parser.Default.ParseArguments(args, commandTypes);
                    foreach (var command in Commands)
                    {
                        command.Register(ref parseResult);
                    }
                }
                catch (HeadlessException e)
                {
                    Log.Fatal(e.Message);
                    exitCode = 1;
                }
                catch (Exception e)
                {
                    var errString = $"{e.Message}\n{e.StackTrace}";
                    Log.Fatal(errString);
                    exitCode = 1;
                }
                finally
                {
                    ConsoleUtil.FreeConsole();
                }

                return(exitCode);
            }
        }