Exemplo n.º 1
0
        public void Start(string[] args)
        {
            var commandMatcher = new CommandMatcher(args);
            var command = _commandFactory.GetCommand(commandMatcher.CommandName);

            if (command != null)
            {
                _commandPropertyWalker.FillCommandProperties(args, command);
            }

            var nonInteractive = command != null && command.NonInteractive;
            var exit = false;

            TryToProceedCommand(command, args);

            while (!nonInteractive && !exit)
            {
                Console.WriteLine("Enter commands or type exit to close");

                if (!nonInteractive)
                {
                    var input = Console.ReadLine();

                    if (!string.IsNullOrEmpty(input))
                    {
                        if (input.ToLower() == "exit")
                            exit = true;
                        else
                        {
                            var tempArgs = input.Split(' ');
                            command = _commandFactory.GetCommand(new CommandMatcher(tempArgs).CommandName);
                            if (TryToProceedCommand(command, tempArgs))
                                nonInteractive = command.NonInteractive;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Start(string[] args)
        {
            Func<string, string[]> splitCommand = input => input.Split(new[]{" -"}, StringSplitOptions.None);

            //The standard argument parsing does not handle inputs like --logLevel=[Error, Information] as we want them
            //to be treated. That's why we first add a whitespace to each fragment, then combine them again, and
            //then split them again the way we want it.
            args = splitCommand(string.Concat(args.Select(fragment => " " + fragment)));

            WriteGreeting();

            if (args.Contains("-debug"))
            {
                Console.WriteLine("Attach a debugger and hit any key to continue");
                Console.ReadLine();
            }

            var commandMatcher = new CommandMatcher(args);
            var command = _commandFactory.GetCommand(commandMatcher.CommandName);

            if (command != null)
            {
                try
                {
                    TryToProceedCommand(command, args);
                }
                catch (Exception exception)
                {
                    LogException(new TraceConsole(Console,command), exception, command);
                }
            }
            else
                NotifyOnUnknownCommand(commandMatcher.CommandName);

            var nonInteractive = command != null && command.NonInteractive;
            var exit = false;

            while (!nonInteractive && !exit)
            {
                if (!nonInteractive)
                {
                    var input = Console.ReadLine().Trim();

                    if (!string.IsNullOrEmpty(input))
                    {
                        _history.Add(input);

                        if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
                            exit = true;
                        else if(input.Equals("clear history", StringComparison.OrdinalIgnoreCase))
                        {
                            _history.DeleteEntireHistory();
                            ConsoleHelper.WriteLineInGreen(Console,"Deleted all history entries");
                        }
                        else if(input.Equals("list commands", StringComparison.OrdinalIgnoreCase))
                        {
                            _commandFactory.GetAvailable().ForEach(c => ConsoleHelper.WriteLineInGreen(Console, c.Name));
                        }
                        else
                        {
                            var tempArgs = splitCommand(input);
                            var commandName = new CommandMatcher(tempArgs).CommandName;
                            command = _commandFactory.GetCommand(commandName);

                            if (command == null)
                                NotifyOnUnknownCommand(commandName);

                            if (TryToProceedCommand(command, tempArgs))
                                nonInteractive = command.NonInteractive;
                        }
                    }

                    _history.ResetHistoryMarker();
                }
            }
        }