/// <summary>
        /// This method exists to validate documentation for cases where config happens external to AppRunner.Run
        /// </summary>
        public static (int exitCode, string output) InvokeMainMethod(this Type type, string args)
        {
            string[] argsArray  = new CommandLineStringSplitter().SplitToArray(args);
            var      mainMethod = type.GetMethod("Main", BindingFlags.Static | BindingFlags.NonPublic);

            Assert.IsNotNull(mainMethod);

            return(CaptureConsoleWrites(() => (int)mainMethod !.Invoke(null, new object[] { argsArray }) !));
        }
Ejemplo n.º 2
0
        public void Start()
        {
            var console           = _context.Console;
            var cancellationToken = _context.CancellationToken;

            bool pressedCtrlC = false;

            Console.CancelKeyPress += (sender, args) =>
            {
                pressedCtrlC = true;
            };

            PrintSessionInit();

            bool pendingNewLine = false;

            void Write(string?value = null)
            {
                console !.Write(value);
                pendingNewLine = true;
            }

            void WriteLine(string?value = null)
            {
                console !.WriteLine(value);
                pendingNewLine = false;
            }

            void EnsureNewLine()
            {
                if (pendingNewLine)
                {
                    WriteLine();
                }
            }

            while (!cancellationToken.IsCancellationRequested)
            {
                EnsureNewLine();
                Write(">>>");
                var input = console.In.ReadLine();
                if (input is null || pressedCtrlC)
                {
                    pressedCtrlC = false;
                    WriteLine();
                    return;
                }

                var args = new CommandLineStringSplitter().Split(input).ToArray();
                if (args.Length == 0)
                {
                    WriteLine();
                    continue;
                }
                if (args.Length == 1)
                {
                    var singleArg = args[0];
                    switch (singleArg)
                    {
                    case "exit":
                    case "quit":
                        return;

                    case "help":
                        PrintSessionHelp();
                        continue;
                    }
                    if (singleArg == Environment.NewLine)
                    {
                        WriteLine();
                        continue;
                    }
                }
                EnsureNewLine();
                _appRunner.Run(args);
            }
            EnsureNewLine();
        }