Exemple #1
0
        public void ProcessCommand(IWrite iWrite, string cmdName)
        {
            Command cmd;
            // store the current menu
            Menu currentMenuCopy    = CurrentMenu;
            bool restoreCurrentMenu = false;

            if (cmdName.Equals(""))
            {
                return;
            }

            cmdName = OutputUtils.RemoveLeadingBlanks(cmdName);

            // is there a slash ? fectch the command before the slash
            // and execute the command after that remove the prefix from the string
            // CurrentMenu reference will be restored from copy in the end of the function
            int argIdx = cmdName.IndexOf(" ");                   // ignore slashes in the arguments

            if (argIdx < 0)
            {
                argIdx = cmdName.Length;                         // if there is no arguments look until end of line
            }
            int slashIdx = cmdName.IndexOf("/");

            while ((slashIdx >= 0) && (slashIdx < argIdx))
            {
                string subcommand = cmdName.Substring(0, slashIdx);
                ProcessCommand(iWrite, subcommand);
                cmdName            = cmdName.Remove(0, slashIdx + 1);
                slashIdx           = cmdName.IndexOf("/");
                restoreCurrentMenu = true;
            }

            // may be exit or help commands - always look in the system menu
            bool found = SystemMenu.FindCommand(cmdName, out cmd);

            // not a system command try current menu then
            if (!found)
            {
                found = CurrentMenu.FindCommand(cmdName, out cmd);
            }

            if (found && cmd.IsCommand())
            {
                // temporary no parsing for the arguments
                cmd.Handler(iWrite, cmdName, SplitCommand(cmdName));
            }
            else if (found && !cmd.IsCommand())
            {
                CurrentMenu = (Menu)cmd;
                PrintCommands(iWrite);
            }
            else
            {
                iWrite.WriteLine("Unknown command " + cmdName);
            }

            if (restoreCurrentMenu)
            {
                CurrentMenu = currentMenuCopy;
            }
        }