Пример #1
0
            public static CommandBase Create(String commandLine)
            {
                CommandBase res = null;

                Match m = _exCmdLine.Match(commandLine);

                if (!m.Success)
                {
                    throw new ApplicationException(String.Format("Unrecognized command format: '{0}'.", commandLine));
                }

                String commandName = m.Groups["cmd"].Value.ToLower();
                Type   commandT;

                if (!_commandsDescr.TryGetValue(commandName, out commandT))
                {
                    throw new ApplicationException(String.Format("The command '{0}' is unknown.", commandName));
                }

                res = (CommandBase)Activator.CreateInstance(commandT, true);


                Int32 numArgs = m.Groups["arg"].Captures.Count;

                if (numArgs < res.NumberArgsMin || numArgs > res.NumberArgsMax)
                {
                    throw new ApplicationException(String.Format("The command '{0}' has improper number of arguments. Must be {1:d} <= N <= {2:d}.", commandLine, res.NumberArgsMin, res.NumberArgsMax));
                }

                res._arguments = new ArgInfo[numArgs];
                Int32 i = 0;

                foreach (Capture arg in m.Groups["arg"].Captures)
                {
                    String pathArg = arg.Value.Trim();
                    if (!_exPath.IsMatch(pathArg))
                    {
                        throw new ApplicationException(String.Format("Argument has unrecognized characters: '{0}'.", arg.Value));
                    }
                    res._arguments[i++] = new ArgInfo(pathArg);
                }

                res.Construct();

                return(res);
            }