protected virtual void ParseAttribute(ConsoleCommandAttribute attr, MethodInfo method)
        {
            ParameterInfo[] parameters = method.GetParameters();

            attr.help = GenerateHelpForAttribute(attr, parameters);

            // Add it to the commands
            commands.Add(attr, method.ContainsGenericParameters ? method.MakeGenericMethod(typeof(object)) : method);
        }
Пример #2
0
        public ConsoleCommand([NotNull] ConsoleCommandAttribute attribute, [NotNull] MethodInfo method)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            command = attribute.command.ToLower();
            help    = attribute.help;
            excludeFromCommandList = attribute.excludeFromCommandList;
            showTypesInUsage       = attribute.showTypesInUsage;
            this.method            = method;
            ParametersFromMethodInfo();
        }
        protected virtual string GenerateHelpForAttribute(ConsoleCommandAttribute attr, ParameterInfo[] parameters)
        {
            string help = attr.help ?? string.Empty;

            if (parameters.Length > 0)
            {
                if (!string.IsNullOrEmpty(help))
                {
                    help += "\n";
                }

                help += string.Format("Usage:\n\t{0}", attr.command);

                // Go through all the parameters of the method
                foreach (ParameterInfo parameterInfo in parameters)
                {
                    help += GenerateHelpFromParameter(attr, parameterInfo);
                }
            }

            return(help);
        }
        protected virtual string GenerateHelpFromParameter(ConsoleCommandAttribute attr, ParameterInfo parameter)
        {
            string help = " <";

            help += parameter.Name;

            // Add some extra info for some types
            if (parameter.ParameterType == typeof(bool))
            {
                help += " (true|false)";
            }
            else if (parameter.ParameterType.IsEnum)
            {
                help += string.Format(" ({0})", string.Join("|", Enum.GetNames(parameter.ParameterType)));
            }
            else if (attr.showTypesInUsage)
            {
                help += string.Format(" ({0})", parameter.ParameterType.Name);
            }

            help += ">";

            return(help);
        }