Esempio n. 1
0
        internal IEnumerator <string> Complete(string partialCommand, string lastWord)
        {
            string[] st   = partialCommand.Split(' ');
            String   cmd  = (String)st[0];
            int      argc = st.Length;

            // 'aliases' command gets no names.
            string[] commandNames1 = dispatcher.GetCommandNames(typeof(AliasesCommand));
            if (Array.BinarySearch(commandNames1, cmd) >= 0)
            {
                return(null);
            }

            // some completion within the alias/unalias commands.
            commandNames1 = dispatcher.GetCommandNames(typeof(AliasCommand));
            string[] commandNames2 = dispatcher.GetCommandNames(typeof(UnaliasCommand));
            if (Array.BinarySearch(commandNames1, cmd) >= 0 ||
                Array.BinarySearch(commandNames2, cmd) >= 0)
            {
                List <string> alreadyGiven = new List <string>();

                if (Array.BinarySearch(commandNames1, cmd) >= 0)
                {
                    // do not complete beyond first word.
                    if (argc > ("".Equals(lastWord) ? 0 : 1))
                    {
                        return(null);
                    }
                }
                else
                {
                    /*
                     * remember all aliases, that have already been given on
                     * the commandline and exclude from completion..
                     * cool, isn't it ?
                     */
                    for (int i = 1; i < st.Length; i++)
                    {
                        alreadyGiven.Add(st[i]);
                    }
                }

                // ok, now return the list.
                return(new AliasSortedMatchEnumerator(alreadyGiven, lastWord, aliases));
            }

            /* ok, someone tries to complete something that is a command.
             * try to find the actual command and ask that command to do
             * the completion.
             */
            String toExecute = (String)aliases[cmd];

            if (toExecute != null)
            {
                Command c = dispatcher.GetCommand(toExecute);
                if (c != null)
                {
                    int    i     = 0;
                    String param = partialCommand;
                    while (param.Length < i &&
                           Char.IsWhiteSpace(param[i]))
                    {
                        ++i;
                    }
                    while (param.Length < i &&
                           !Char.IsWhiteSpace(param[i]))
                    {
                        ++i;
                    }
                    return(c.Complete(dispatcher, toExecute + param.Substring(i), lastWord));
                }
            }

            return(null);
        }
Esempio n. 2
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            if (args.Count > 1)
            {
                return(CommandResultCode.SyntaxError);
            }

            string commandName = null;

            if (args.MoveNext())
            {
                commandName = args.Current;
            }

            // nothing given: provide generic help.

            Application.Error.WriteLine();

            int maxPad = 0;

            if (commandName == null)
            {
                ICollection <Command> commands = Application.Commands.RegisteredCommands;

                // process the command groups first...
                Dictionary <string, List <CommandHelp> > groups = new Dictionary <string, List <CommandHelp> >();
                foreach (Command command in commands)
                {
                    string groupName = command.GroupName;
                    if (groupName == null || groupName.Length == 0)
                    {
                        groupName = "commands";
                    }

                    List <CommandHelp> list;
                    if (!groups.TryGetValue(groupName, out list))
                    {
                        list = new List <CommandHelp>();
                        groups[groupName] = list;
                    }

                    CommandHelp commandHelp = new CommandHelp();

                    StringBuilder cmdPrint = new StringBuilder(" ");
                    string[]      aliases  = command.Aliases;

                    cmdPrint.Append(command.Name);

                    if (aliases != null && aliases.Length > 0)
                    {
                        cmdPrint.Append(" | ");
                        for (int i = 0; i < aliases.Length; i++)
                        {
                            if (i != 0)
                            {
                                cmdPrint.Append(" | ");
                            }
                            cmdPrint.Append(aliases[i]);
                        }
                    }

                    commandHelp.Name = cmdPrint.ToString();

                    string description = command.ShortDescription;
                    if (description == null)
                    {
                        // no description ... try to get the groups description...
                    }

                    commandHelp.Description = description;

                    maxPad = Math.Max(maxPad, cmdPrint.Length);

                    list.Add(commandHelp);
                }

                foreach (KeyValuePair <string, List <CommandHelp> > entry in groups)
                {
                    string groupName = entry.Key;
                    Application.Error.Write(groupName);
                    Application.Error.Write(":");
                    Application.Error.WriteLine();

                    List <CommandHelp> commandList = entry.Value;
                    foreach (CommandHelp command in commandList)
                    {
                        Application.Error.Write("  ");
                        Application.Error.Write(command.Name);

                        if (command.Description != null)
                        {
                            for (int i = 0; i < maxPad - command.Name.Length; ++i)
                            {
                                Application.Error.Write(" ");
                            }

                            Application.Error.Write(" : ");
                            Application.Error.Write(command.Description);
                        }

                        Application.Error.WriteLine();
                    }
                }
            }
            else
            {
                CommandDispatcher disp = Application.Commands;

                string  cmdString = disp.CompleteCommandName(commandName);
                Command c         = disp.GetCommand(cmdString);
                if (c == null)
                {
                    Application.Error.WriteLine("Help: unknown command '" + cmdString + "'");
                    Application.Error.WriteLine();
                    return(CommandResultCode.ExecutionFailed);
                }

                WriteDescription(c);
            }

            Application.Error.WriteLine();
            return(CommandResultCode.Success);
        }