Beispiel #1
0
        private ArgumentMap Map(object target, PropertyInfo property)
        {
            var    attribute          = ReflectionCompatibility.GetCustomAttribute <ArgumentAttribute>(property);
            string longName           = null;
            char?  shortName          = null;
            string helpText           = null;
            bool   showHelpComplement = true;
            int?   position           = null;
            bool   hasPosition        = false;
            bool   isOptional         = true;
            bool   hasDefaultValue    = false;
            object defaultValue       = null;

            if (attribute != null)
            {
                longName           = attribute.LongName;
                shortName          = attribute.ShortName;
                helpText           = attribute.Help;
                showHelpComplement = attribute.ShowHelpComplement;
                hasPosition        = attribute.HasPosition;
                position           = (int?)attribute.Position;
                hasDefaultValue    = attribute.HasDefaultValue;
                defaultValue       = attribute.DefaultValue;
                isOptional         = !attribute.IsRequired;
            }

            return(Map(target, property, property.Name, property.PropertyType, longName, shortName, hasPosition, position, helpText, showHelpComplement, isOptional, hasDefaultValue, defaultValue));
        }
Beispiel #2
0
        private ArgumentMap Map(object target, ParameterInfo parameter)
        {
            var attribute = ReflectionCompatibility.GetCustomAttribute <ArgumentAttribute>(parameter);

            string longName           = null;
            char?  shortName          = null;
            string helpText           = null;
            bool   showHelpComplement = true;
            int?   position           = null;
            bool   hasPosition        = false;
            bool   isOptional         = parameter.IsOptional;
            bool   hasDefaultValue    = parameter.HasDefaultValue;
            object defaultValue       = parameter.HasDefaultValue ? parameter.DefaultValue : null;

            if (attribute != null)
            {
                longName           = attribute.LongName;
                shortName          = attribute.ShortName;
                helpText           = attribute.Help;
                showHelpComplement = attribute.ShowHelpComplement;
                hasPosition        = attribute.HasPosition;
                position           = (int?)attribute.Position;
            }

            return(this.Map(target, parameter, parameter.Name, parameter.ParameterType, longName, shortName, hasPosition, position, helpText, showHelpComplement, isOptional, hasDefaultValue, defaultValue));
        }
Beispiel #3
0
        /// <summary>
        /// Create a map of arguments from the specific target object
        /// </summary>
        /// <param name="target">Object to be mapped</param>
        /// <param name="properties">Properties to be mapped</param>
        /// <param name="onlyWithAttribute">Ignore all properties that do not have the ArgumentAttribute</param>
        /// <returns>List of ArgumentMap</returns>
        public IEnumerable <ArgumentMap> Map(object target, PropertyInfo[] properties, bool onlyWithAttribute = false)
        {
            var maps = new List <ArgumentMap>();

            foreach (PropertyInfo property in properties)
            {
                var attribute = ReflectionCompatibility.GetCustomAttribute <ArgumentAttribute>(property);

                if (onlyWithAttribute && attribute == null)
                {
                    continue;
                }

                var map = Map(target, property);
                maps.Add(map);
            }

            Validate(maps, target.GetType().Name);

            return(SortByPosition(maps));
        }
Beispiel #4
0
        /// <summary>
        /// Create a map of actions from the specific target object
        /// </summary>
        /// <param name="target">Object to be mapped</param>
        /// <param name="methods">Methods to be mapped</param>
        /// <param name="onlyWithAttribute">Ignore all methods that do not have the ActionAttribute</param>
        /// <param name="usePrefixInAllMethods">Determine whether the methods are prefixed</param>
        /// <param name="prefix">Prefix text is enabled</param>
        /// <returns>List of ActionMap</returns>
        public IEnumerable <ActionMap> Map(object target, MethodInfo[] methods, bool onlyWithAttribute = false, bool usePrefixInAllMethods = false, string prefix = null)
        {
            var maps = new List <ActionMap>();

            foreach (var method in methods)
            {
                var attribute = ReflectionCompatibility.GetCustomAttribute <ActionAttribute>(method);

                var isMainMethod    = method.Name.ToLower() == Executor.MAIN_METHOD_NAME;
                var countParameters = method.GetParameters().Length;

                // the main method, with zero arguments, can't be a action
                if (isMainMethod && countParameters == 0 && attribute == null)
                {
                    continue;
                }
                else if (isMainMethod && countParameters == 0 && attribute != null)
                {
                    throw new Exception("The main method, with zero arguments, can be se a action. This method is reserved to be aways invoked in action.   it is defined.");
                }

                var ignoredByWithoutAttr = onlyWithAttribute && attribute == null && !isMainMethod;
                var ignoredByMethod      = attribute != null && attribute.Ignore;

                if (ignoredByWithoutAttr || ignoredByMethod)
                {
                    continue;
                }

                string actionNameRaw;
                string actionName;

                if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Name))
                {
                    actionNameRaw = attribute.Name;
                }
                else
                {
                    actionNameRaw = argumentMapper.GetLongName(method.Name);
                }

                var usePrefix      = attribute == null ? true : attribute.UsePrefix;
                var usePrefixFinal = usePrefixInAllMethods && usePrefix;
                if (usePrefixFinal)
                {
                    if (string.IsNullOrWhiteSpace(prefix))
                    {
                        prefix = argumentMapper.GetPrefixByType(target.GetType());
                    }

                    actionName = prefix + "-" + actionNameRaw;
                }
                else
                {
                    actionName = actionNameRaw;
                }

                var isDefaultAction = false;
                if (isMainMethod || (attribute != null && attribute.IsDefault))
                {
                    isDefaultAction = true;
                }

                var helpText             = attribute == null ? null : attribute.Help;
                var enablePositionalArgs = attribute == null ? true : attribute.EnablePositionalArgs;

                var argsMaps = this.argumentMapper.GetFromMethod(target, method);
                maps.Add(new ActionMap(target, method, actionName, prefix, actionNameRaw, usePrefixFinal, helpText, isDefaultAction, enablePositionalArgs, argsMaps));
            }

            return(maps);
        }