Ejemplo n.º 1
0
 private static void ParseCommand(string[] args, ref int i)
 {
     if (args.Length == 0)
     {
         SetCommand(new ShowHelpCommand());
     }
     else if (args[0].Equals("help", StringComparison.OrdinalIgnoreCase) || args[0].Equals("?", StringComparison.OrdinalIgnoreCase))
     {
         SetCommand(new ShowHelpCommand(TryParseString(args, ref i), false));
     }
     else
     {
         if (!IsCommand(args[i]))
         {
             throw new CommandParserException("Invalid argument '" + args[i] + "'.");
         }
         var a = args[i].Substring(1).ToLowerInvariant();
         if (string.Equals(a, "a", StringComparison.Ordinal) || string.Equals(a, "attach", StringComparison.Ordinal))
         {
             string filePath = ParseString(args, ref i, a);
             string dbName   = TryParseString(args, ref i);
             SetCommand(new AttachDatabaseCommand(filePath, dbName));
         }
         else if (string.Equals(a, "create", StringComparison.Ordinal))
         {
             string str4         = ParseString(args, ref i, a);
             string databaseName = TryParseString(args, ref i);
             SetCommand(new CreateDatabaseCommand(str4, databaseName));
         }
         else if (string.Equals(a, "l", StringComparison.Ordinal) || string.Equals(a, "list", StringComparison.Ordinal))
         {
             SetCommand(new ListDatabasesCommand());
         }
         else if (string.Equals(a, "childlist", StringComparison.Ordinal))
         {
             SetCommand(new ListChildInstancesCommand());
         }
         else if (string.Equals(a, "d", StringComparison.Ordinal) || string.Equals(a, "detach", StringComparison.Ordinal))
         {
             SetCommand(new DetachDatabasesCommand(ParseString(args, ref i, a)));
         }
         else if (string.Equals(a, "u", StringComparison.Ordinal) || string.Equals(a, "upgrade", StringComparison.Ordinal))
         {
             SetCommand(new UpgradeDatabasesCommand(ParseString(args, ref i, a)));
         }
         else if (string.Equals(a, "сс", StringComparison.Ordinal) || string.Equals(a, "concat", StringComparison.Ordinal))
         {
             var _concatConfig = ParseString(args, ref i, a);
             SetCommand(new ConcatCommand(_concatConfig));
         }
         else if (string.Equals(a, "generatescript", StringComparison.Ordinal) || string.Equals(a, "g", StringComparison.Ordinal))
         {
             string dbName   = ParseString(args, ref i, a);
             string prefix   = ParseString(args, ref i, a);
             string filePath = TryParseString(args, ref i);
             SetCommand(new CreateScripCommand(filePath, dbName, prefix));
         }
         else if (string.Equals(a, "t", StringComparison.Ordinal) || string.Equals(a, "trace", StringComparison.Ordinal))
         {
             bool flag;
             ParseString(args, ref i, a);
             if (args[i][0] == '+')
             {
                 flag = true;
             }
             else
             {
                 if (args[i][0] != '-')
                 {
                     throw new CommandParserInvalidArgumentsException(a);
                 }
                 flag = false;
             }
             int result = -1;
             if ((args[i].Length > 1) && !int.TryParse(args[i].Substring(1), out result))
             {
                 throw new CommandParserInvalidArgumentsException(a);
             }
             SetCommand(new EnableTracingCommand(flag, result));
         }
         else if (string.Equals(a, "c", StringComparison.Ordinal) || string.Equals(a, "console", StringComparison.Ordinal))
         {
             SetCommand(new RunConsoleCommand());
         }
         else if (string.Equals(a, "run", StringComparison.Ordinal))
         {
             string scriptPath             = ParseString(args, ref i, a);
             string rawVariableDeclaration = TryParseString(args, ref i);
             if (rawVariableDeclaration != null)
             {
                 Dictionary <string, string> variables = null;
                 if (!RunCommandFileCommand.TryParseVariableDeclaration(rawVariableDeclaration, out variables))
                 {
                     throw new CommandParserInvalidArgumentsException(a);
                 }
                 SetCommand(new RunCommandFileCommand(scriptPath, variables));
             }
             else
             {
                 SetCommand(new RunCommandFileCommand(scriptPath, null));
             }
         }
         else if (string.Equals(a, "version", StringComparison.Ordinal))
         {
             SetCommand(new ShowVersionCommand());
         }
         else if (string.Equals(a, "listsrv", StringComparison.Ordinal))
         {
             int  num2;
             bool listRemote = false;
             i = num2 = i + 1;
             if (args.Length > num2)
             {
                 if (string.Compare(args[i], "remote", StringComparison.OrdinalIgnoreCase) != 0)
                 {
                     throw new CommandParserInvalidArgumentsException(a);
                 }
                 listRemote = true;
             }
             SetCommand(new ListServersCommand(listRemote));
         }
         else if (string.Equals(a, "shrink", StringComparison.Ordinal))
         {
             SetCommand(new ShrinkDatabaseCommand(ParseString(args, ref i, a)));
         }
         else if (string.Equals(a, "s", StringComparison.Ordinal) || string.Equals(a, "server", StringComparison.Ordinal))
         {
             _connectionOptions.ServerName = ParseString(args, ref i, a);
         }
         else if (string.Equals(a, "user", StringComparison.Ordinal))
         {
             _connectionOptions.UserName = ParseString(args, ref i, a);
         }
         else if (string.Equals(a, "pwd", StringComparison.Ordinal))
         {
             _connectionOptions.Password = ParseString(args, ref i, a);
         }
         else if (string.Equals(a, "pwd?", StringComparison.Ordinal))
         {
             _connectionOptions.PromptForPassword = true;
         }
         else if (string.Equals(a, "timeout", StringComparison.Ordinal))
         {
             int num3;
             i = num3 = i + 1;
             if ((args.Length <= num3) || !int.TryParse(args[i], out _connectionOptions.ConnectionTimeout))
             {
                 throw new CommandParserInvalidArgumentsException(a);
             }
         }
         else if (string.Equals(a, "cmdtimeout", StringComparison.Ordinal))
         {
             int num4;
             i = num4 = i + 1;
             if ((args.Length <= num4) || !int.TryParse(args[i], out _connectionOptions.CommandTimeout))
             {
                 throw new CommandParserInvalidArgumentsException(a);
             }
         }
         else if (string.Equals(a, "m", StringComparison.Ordinal) || string.Equals(a, "main", StringComparison.Ordinal))
         {
             _connectionOptions.UseMainInstance = true;
         }
         else if (string.Equals(a, "child", StringComparison.Ordinal))
         {
             int num5;
             _connectionOptions.UseMainInstance = false;
             i = num5 = i + 1;
             if (args.Length > num5)
             {
                 if (!args[i].StartsWith("-") && !args[i].StartsWith("/"))
                 {
                     _connectionOptions.RunningAs = args[i];
                 }
                 else
                 {
                     i--;
                 }
             }
         }
         else if (string.Equals(a, "log", StringComparison.Ordinal))
         {
             _logFilePath = ParseString(args, ref i, a);
         }
         else if (string.Equals(a, "nocode", StringComparison.Ordinal))
         {
             Global.NoCode = true;
         }
         else if (string.Equals(a, "silentmode", StringComparison.Ordinal))
         {
             Global.SilentMode = true;
         }
         else
         {
             if ((!string.Equals(a, "?", StringComparison.Ordinal) && !string.Equals(a, "help", StringComparison.Ordinal)) && !string.Equals(a, "-help", StringComparison.OrdinalIgnoreCase))
             {
                 throw new CommandParserInvalidCommandException(a);
             }
             SetCommand(new ShowHelpCommand(TryParseString(args, ref i), false));
         }
     }
 }
Ejemplo n.º 2
0
        internal static ICommand Parse(string commandLine)
        {
            string str;

            string[] strArray;
            if (commandLine == null)
            {
                throw new CommandParserException("Invalid null command line.");
            }
            if (!commandLine.StartsWith(Settings.ConsoleCommandPrefix))
            {
                throw new CommandParserException("Commands should start with a '!' character.");
            }
            int index = commandLine.IndexOf(" ");

            if (index == -1)
            {
                str      = commandLine.Substring(1).ToLowerInvariant();
                strArray = new string[0];
            }
            else
            {
                int startIndex = 1;
                str      = commandLine.Substring(startIndex, index - startIndex).ToLowerInvariant();
                strArray = ConsoleUtil.ParseCommandArgs(commandLine.Substring(index + 1));
            }
            if (string.Compare(str, "attach", StringComparison.Ordinal) == 0)
            {
                string filePath = (strArray.Length > 0) ? strArray[0] : null;
                string dbName   = (strArray.Length > 1) ? strArray[1] : null;
                if (filePath == null)
                {
                    throw new CommandParserInvalidArgumentsException(str);
                }
                return(new AttachDatabaseCommand(filePath, dbName));
            }
            if (string.Compare(str, "detach", StringComparison.Ordinal) == 0)
            {
                string database = (strArray.Length > 0) ? strArray[0] : null;
                if (database == null)
                {
                    throw new CommandParserInvalidArgumentsException(str);
                }
                return(new DetachDatabasesCommand(database));
            }
            if (string.Compare(str, "create", StringComparison.Ordinal) == 0)
            {
                string str5         = (strArray.Length > 0) ? strArray[0] : null;
                string databaseName = (strArray.Length > 1) ? strArray[1] : null;
                if (str5 == null)
                {
                    throw new CommandParserInvalidArgumentsException(str);
                }
                return(new CreateDatabaseCommand(str5, databaseName));
            }
            if (string.Compare(str, "trace", StringComparison.Ordinal) == 0)
            {
                bool flag;
                if (strArray.Length != 1)
                {
                    throw new CommandParserInvalidArgumentsException(str);
                }
                if (strArray[0][0] == '+')
                {
                    flag = true;
                }
                else
                {
                    if (strArray[0][0] != '-')
                    {
                        throw new CommandParserInvalidArgumentsException(str);
                    }
                    flag = false;
                }
                int result = -1;
                if ((strArray[0].Length > 1) && !int.TryParse(strArray[0].Substring(1), out result))
                {
                    throw new CommandParserInvalidArgumentsException(str);
                }
                return(new EnableTracingCommand(flag, result));
            }
            if (string.Compare(str, "commandtimeout", StringComparison.Ordinal) == 0)
            {
                int    num4;
                string str7 = (strArray.Length > 0) ? strArray[0] : null;
                if (str7 == null)
                {
                    throw new CommandParserInvalidArgumentsException(str);
                }
                if (!int.TryParse(str7, out num4))
                {
                    throw new CommandParserInvalidArgumentsException(str);
                }
                return(new CommandTimeoutCommand(num4));
            }
            if (string.Compare(str, "list", StringComparison.Ordinal) == 0)
            {
                return(new ListDatabasesCommand());
            }
            if (string.Compare(str, "run", StringComparison.Ordinal) == 0)
            {
                string scriptPath = (strArray.Length > 0) ? strArray[0] : null;
                string str9       = (strArray.Length > 1) ? strArray[1] : null;
                if (scriptPath == null)
                {
                    throw new CommandParserInvalidArgumentsException(str);
                }
                Dictionary <string, string> variables = null;
                if (!string.IsNullOrEmpty(str9) && !RunCommandFileCommand.TryParseVariableDeclaration(str9, out variables))
                {
                    throw new Exception("Invalid variable declaration. Must be in the form name=value,name2=value2,...");
                }
                return(new RunCommandFileCommand(scriptPath, variables));
            }
            if (string.Compare(str, "timer", StringComparison.Ordinal) == 0)
            {
                return(new TimerCommand());
            }
            if (string.Compare(str, "history", StringComparison.Ordinal) != 0)
            {
                if (string.Compare(str, "logopen", StringComparison.Ordinal) == 0)
                {
                    string str10 = (strArray.Length > 0) ? strArray[0] : null;
                    if (str10 == null)
                    {
                        throw new CommandParserInvalidArgumentsException(str);
                    }
                    if (string.IsNullOrEmpty(str10))
                    {
                        throw new CommandParserException("Expected log file path as argument.");
                    }
                    return(new OpenLogCommand(str10));
                }
                if (string.Compare(str, "logclose", StringComparison.Ordinal) == 0)
                {
                    return(new CloseLogCommand());
                }
                if (string.Compare(str, "consolewnd", StringComparison.Ordinal) != 0)
                {
                    throw new CommandParserInvalidCommandException(str);
                }
                return(new ConsoleWindowCommand());
            }
            string str11 = (strArray.Length > 0) ? strArray[0].ToLowerInvariant() : null;
            string s     = (strArray.Length > 1) ? strArray[1] : null;
            string str13 = str11;

            if (str13 == null)
            {
                throw new CommandParserInvalidArgumentsException(str);
            }
            if (!(str13 == "show"))
            {
                if (str13 != "save")
                {
                    if (str13 != "clear")
                    {
                        throw new CommandParserInvalidArgumentsException(str);
                    }
                    return(new ClearHistoryCommand());
                }
            }
            else
            {
                int num5 = 0;
                if ((s != null) && (!int.TryParse(s, out num5) || (num5 < 0)))
                {
                    throw new CommandParserInvalidArgumentsException(str);
                }
                return(new ShowHistoryCommand(num5));
            }
            if (s == null)
            {
                throw new CommandParserInvalidArgumentsException(str);
            }
            return(new SaveHistoryCommand(s));
        }