Exemplo n.º 1
0
        private bool CanExecute <T>(int argsCount) where T : class
        {
            Type type = typeof(T);

            List <MethodInfo> methods = CClassUtils.ListInstanceMethods(type, delegate(MethodInfo method)
            {
                return(method.Name.Equals("Execute"));
            });

            Assert.AreEqual(1, methods.Count);

            return(CCommandUtils.CanInvokeMethodWithArgsCount(methods[0], argsCount));
        }
Exemplo n.º 2
0
        private MethodInfo resolveExecuteMethod(String[] args)
        {
            if (executeMethods == null)
            {
                executeMethods = resolveExecuteMethods();
            }

            MethodInfo method = null;

            foreach (MethodInfo m in executeMethods)
            {
                if (CCommandUtils.CanInvokeMethodWithArgsCount(m, args.Length))
                {
                    if (method != null) // multiple methods found
                    {
                        return(null);
                    }

                    method = m;
                }
            }

            return(method);
        }
Exemplo n.º 3
0
        protected virtual String[] GetUsageArgs()
        {
            if (m_values != null && m_values.Length > 0)
            {
                return(new String[] { " " + StringUtils.Join(m_values, "|") });
            }

            if (executeMethods == null)
            {
                executeMethods = resolveExecuteMethods();
            }

            if (executeMethods.Length > 0)
            {
                String[] result = new String[executeMethods.Length];
                for (int i = 0; i < result.Length; ++i)
                {
                    result[i] = CCommandUtils.GetMethodParamsUsage(executeMethods[i]);
                }
                return(result);
            }

            return(null);
        }
Exemplo n.º 4
0
        private void ParseOption(Iterator <string> iter, Option opt) // FIXME: fix '-' and '--' args
        {
            Type type = opt.Target.FieldType;

            opt.IsHandled = true;

            if (type == typeof(int))
            {
                object value = CCommandUtils.NextIntArg(iter);
                CheckValue(opt, value);
                opt.Target.SetValue(this, value);
            }
            else if (type == typeof(bool))
            {
                opt.Target.SetValue(this, true);
            }
            else if (type == typeof(float))
            {
                object value = CCommandUtils.NextFloatArg(iter);
                CheckValue(opt, value);
                opt.Target.SetValue(this, value);
            }
            else if (type == typeof(string))
            {
                string value = CCommandUtils.NextArg(iter);
                CheckValue(opt, value);
                opt.Target.SetValue(this, value);
            }
            else if (type == typeof(string[]))
            {
                string[] arr = (string[])opt.Target.GetValue(this);
                if (arr == null)
                {
                    throw new CCommandException("Field should be initialized: " + opt.Target.Name);
                }

                for (int i = 0; i < arr.Length; ++i)
                {
                    arr[i] = CCommandUtils.NextArg(iter);
                }
            }
            else if (type == typeof(int[]))
            {
                int[] arr = (int[])opt.Target.GetValue(this);
                if (arr == null)
                {
                    throw new CCommandException("Field should be initialized: " + opt.Target.Name);
                }

                for (int i = 0; i < arr.Length; ++i)
                {
                    arr[i] = CCommandUtils.NextIntArg(iter);
                }
            }
            else if (type == typeof(float[]))
            {
                float[] arr = (float[])opt.Target.GetValue(this);
                if (arr == null)
                {
                    throw new CCommandException("Field should be initialized: " + opt.Target.Name);
                }

                for (int i = 0; i < arr.Length; ++i)
                {
                    arr[i] = CCommandUtils.NextFloatArg(iter);
                }
            }
            else if (type == typeof(bool[]))
            {
                bool[] arr = (bool[])opt.Target.GetValue(this);
                if (arr == null)
                {
                    throw new CCommandException("Field should be initialized: " + opt.Target.Name);
                }

                for (int i = 0; i < arr.Length; ++i)
                {
                    arr[i] = CCommandUtils.NextBoolArg(iter);
                }
            }
            else
            {
                throw new CCommandException("Unsupported field type: " + type);
            }
        }
Exemplo n.º 5
0
        private bool ExecuteGuarded(IList <string> tokens, string commandLine = null)
        {
            ResetOptions();

            Iterator <string> iter = new Iterator <string>(tokens);

            iter.Next(); // first token is a command name

            if (this.IsManualMode)
            {
                PrintPrompt(commandLine);
            }

            if (this.IsPlayModeOnly && !Runtime.IsPlaying)
            {
                PrintError("Command is available in the play mode only");
                return(false);
            }

            ReusableList <string> argsList = ReusableLists.NextAutoRecycleList <string>();

            while (iter.HasNext())
            {
                string token = StringUtils.UnArg(iter.Next());

                // first, try to parse options
                if (!TryParseOption(iter, token))
                {
                    // consume the rest of the args
                    argsList.Add(token);
                    while (iter.HasNext())
                    {
                        token = StringUtils.UnArg(iter.Next());
                        argsList.Add(token);
                    }

                    break;
                }
            }

            if (m_values != null)
            {
                if (argsList.Count != 1)
                {
                    PrintError("Unexpected arguments count {0}", argsList.Count);
                    PrintUsage();
                    return(false);
                }

                string arg = argsList[0];
                if (Array.IndexOf(m_values, arg) == -1)
                {
                    PrintError("Unexpected argument '{0}'", arg);
                    PrintUsage();
                    return(false);
                }
            }

            if (m_options != null)
            {
                for (int i = 0; i < m_options.Count; ++i)
                {
                    Option opt = m_options[i];
                    if (opt.IsRequired && !opt.IsHandled)
                    {
                        PrintError("Missing required option --{0}{1}", opt.Name, opt.ShortName != null ? "(-" + opt.ShortName + ")" : "");
                        PrintUsage();
                        return(false);
                    }
                }
            }

            string[]   args          = argsList.Count > 0 ? argsList.ToArray() : EMPTY_COMMAND_ARGS;
            MethodInfo executeMethod = resolveExecuteMethod(args);

            if (executeMethod == null)
            {
                PrintError("Wrong arguments");
                PrintUsage();
                return(false);
            }

            return(CCommandUtils.Invoke(this, executeMethod, args));
        }