Exemplo n.º 1
0
        public void Run()
        {
            var le = new LineEditor(
                Config.ConsoleMode.AppName,
                Config.ConsoleMode.HistorySize.GetValueOrDefault(10))
            {
                AutoCompleteEvent = (text, pos) => GetEntries(text)
            };

            using (CommandsOptions.HideCommandOfType <ConsoleCommand>())
            {
                Writer.WriteLines(
                    "Type ctrl+c to exit.",
                    "Type \"cls\" to clear the console window.",
                    "Type \"> filename\" to redirect output to a file.");

                do
                {
                    string[] args;
                    do
                    {
                        args = le.Edit(Config.ConsoleMode.CommandPromptText + "> ", string.Empty).SplitCmdLineArgs();
                    } while (args.IsNullOrEmpty());

                    if (args[0].Equals("cls", StringComparison.OrdinalIgnoreCase))
                    {
                        Console.Clear();
                    }
                    else if (args[0].Equals(CommandName, StringComparison.OrdinalIgnoreCase))
                    {
                        //already in console mode
                    }
                    else
                    {
                        le.SaveHistory();
                        RunCommand(args);
                    }
                } while (true);
            }
        }
Exemplo n.º 2
0
        private LineEditor.Completion GetEntries(string text)
        {
            string commandName = null;

            string[] parts = null;
            try
            {
                if (_commandCache == null)
                {
                    _commandCache = new Dictionary <string, ConsoleCommandInfo>();
                    foreach (var command in CommandsOptions.GetCommands())
                    {
                        foreach (var name in command.Attribute.PrototypeArray)
                        {
                            _commandCache.Add(name, command);
                        }
                    }
                }

                parts = text.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                commandName = parts.FirstOrDefault();

                if (commandName == null)
                {
                    return(new LineEditor.Completion(string.Empty, new string[0]));
                }

                if (parts.Length == 1)
                {
                    //looking for a command name
                    var commands = _commandCache.Keys
                                   .Where(c => c.StartsWith(commandName, StringComparison.OrdinalIgnoreCase))
                                   .Select(s => s.Substring(commandName.Length) + " ")
                                   .ToArray();
                    return(new LineEditor.Completion(commandName, commands));
                }

                var lastPart = parts.Last().TrimStart(ArgPrefixes);                 //remove the / or - arguments are prefixed for

                List <string> argNames;
                if (!_optionCache.TryGetValue(commandName, out argNames))
                {
                    var commandInfo = _commandCache[commandName];
                    _optionCache[commandName] = argNames = ArgsHelper.GetFlattenedOptionNames(commandInfo.CommandType);
                }

                var options = argNames
                              .Where(a => a.StartsWith(lastPart, StringComparison.OrdinalIgnoreCase))
                              .Where(a => !parts.Contains(a))
                              .Select(s => s.Substring(lastPart.Length) + "=")
                              .ToArray();
                return(new LineEditor.Completion(lastPart, options));
            }
            catch (Exception e)
            {
                e.SetContext("text", text);
                e.SetContext("commandName", commandName);
                e.SetContext("parts", parts);
                throw;
            }
        }