Beispiel #1
0
        private bool ReadFlag(PropertyInfo pi, out object value)
        {
            value = null;
            FlagAttribute fa = pi.GetCustomAttribute <FlagAttribute>( );

            if (fa == null)
            {
                return(false);
            }

            IFlag flag = _spec.Flags.FirstOrDefault(f => f.Name.Equals(fa.Name));

            if (flag == null)
            {
                throw new Exception(
                          $"property {pi.Name} has no corresponding {nameof( IFlag )} with name {fa.Name}");
            }

            if (pi.PropertyType.IsAssignableFrom(typeof(bool)))
            {
                value = _pr.IsSet(flag);
                return(true);
            }

            throw new Exception(
                      $"flag property {pi.Name} should be assignable from bool but is of type {pi.PropertyType}");
        }
Beispiel #2
0
 private void AddFlags(CliSpecification spec, Type t)
 {
     foreach (PropertyInfo pi in t.GetProperties( ))
     {
         FlagAttribute flagAttrib = pi.GetCustomAttribute <FlagAttribute>( );
         if (flagAttrib == null)
         {
             continue;
         }
         AddFlag(spec, t, flagAttrib, pi);
     }
 }
        public void SetFlagValue(MemberInfo member, FlagAttribute flagAttribute)
        {
            var memberType = TypeHelper.GetMemberType(member);

            if (TypeHelper.IsLazy(memberType))
            {
                TypeHelper.SetValue(member, _tArgs, new Lazy <bool>(() => _args.GetFlag(flagAttribute.Name)));
            }
            else
            {
                TypeHelper.SetValue(member, _tArgs, _args.GetFlag(flagAttribute.Name));
            }
        }
Beispiel #4
0
        private void AddFlag(
            CliSpecification spec,
            Type t,
            FlagAttribute flagAttrib,
            PropertyInfo pi)
        {
            Flag flag = new Flag(
                flagAttrib.Name,
                flagAttrib.Optional,
                GetDynamicArgument(spec, t, pi.Name),
                GetArguments(spec, t, pi.Name));

            flag.Description = flagAttrib.Description;
            spec.Flags.Add(flag);
        }
Beispiel #5
0
        /// <summary>
        /// Gets the flags in this command.
        /// </summary>
        protected virtual string GetFlagsString(params string[] ignore)
        {
            StringBuilder builder = new StringBuilder();

            PropertyInfo[] members = this.GetType().GetProperties(BindingFlags.Instance |
                                                                  BindingFlags.Public |
                                                                  BindingFlags.NonPublic);

            foreach (PropertyInfo member in members.Where(t => !(t.GetCustomAttribute <FlagAttribute>() is null)))
            {
                FlagAttribute flag  = member.GetCustomAttribute <FlagAttribute>();
                object        value = member.GetValue(this, null);
                if (value is bool b && b && !ignore.Contains(flag.Name, StringComparer.InvariantCultureIgnoreCase))
                {
                    builder.Append(flag.Name.ToLowerInvariant())
                    .Append(' ');
                }
            }

            return(builder.ToString().Trim());
        }
Beispiel #6
0
        public void RegisterFlag(MemberInfo member, FlagAttribute flagAttribute)
        {
            ValidatePropertySetter(member);
            ValidateFlagMemberType(member);

            var flag = new Flag
            {
                Name                      = flagAttribute.Name,
                Abr                       = flagAttribute.Abr,
                Description               = flagAttribute.Description,
                ValueType                 = typeof(bool),
                DefaultValue              = flagAttribute.DefaultValue,
                EnvironmentVariable       = flagAttribute.EnvironmentVariable,
                IsEnvironmentVariableLazy = TypeHelper.IsLazy(TypeHelper.GetMemberType(member)),
                ConfigPath                = flagAttribute.ConfigPath,
                IsConfigPathLazy          = TypeHelper.IsLazy(TypeHelper.GetMemberType(member)),
                Group                     = flagAttribute.Group
            };

            _commandConfigurator.RegisterFlagInstance(flag);
        }
Beispiel #7
0
        public IItem GetItem(string propertyName)
        {
            PropertyInfo pi = _t.GetProperty(propertyName);

            if (pi == null)
            {
                throw new Exception($"{_t} has no property {propertyName}");
            }

            IItem           item;
            OptionAttribute oa = pi.GetCustomAttribute <OptionAttribute>( );
            FlagAttribute   fa = pi.GetCustomAttribute <FlagAttribute>( );

            if (oa != null)
            {
                UnifiedName optionName =
                    _specDeriverConfig.PropertyNamingStyle.FromString(
                        string.IsNullOrEmpty(oa.Name)? pi.Name : oa.Name);
                item = _spec.Options.FirstOrDefault(o => o.Name.Equals(optionName));
                if (item == null)
                {
                    throw new Exception($"no option mapped to property {propertyName}");
                }
            }
            else if (fa != null)
            {
                item = _spec.Flags.FirstOrDefault(f => f.Name == fa.Name);
                if (item == null)
                {
                    throw new Exception($"no flag mapped to property {propertyName}");
                }
            }
            else
            {
                throw new Exception($"no item mapped to property {propertyName}");
            }

            return(item);
        }
Beispiel #8
0
        /// <summary>
        /// Updates the command's property members by referencing the <see cref="Arguments"/> collection.
        /// </summary>
        internal void UpdateArguments()
        {
            Type type = this.GetType();

            PropertyInfo[] members = type.GetProperties(BindingFlags.Instance |
                                                        BindingFlags.Public |
                                                        BindingFlags.NonPublic);

            List <string> args = this.Arguments.Where(a => a.IndexOf('=') < 0).ToList();

            args.RemoveAll(String.IsNullOrWhiteSpace);

            // get arguments without flags
            foreach (PropertyInfo member in members.Where(prop => !(prop.GetCustomAttribute <FlagAttribute>() is null))
                     .Where(prop => prop.PropertyType == typeof(bool)))
            {
                FlagAttribute flag = member.GetCustomAttribute <FlagAttribute>();
                if (args.Select(a => a.ToUpperInvariant()).Contains(flag.Name.ToUpperInvariant()))
                {
                    args.RemoveAll(a => a.Equals(flag.Name, StringComparison.InvariantCultureIgnoreCase));
                }
            }

            this.Arguments = args.AsReadOnly();

            foreach (PropertyInfo member in members.Where(prop => !(prop.ToVpParameter() is null))
                     .OrderBy(prop => prop.ToVpParameter().Index))
            {
                ParameterAttribute attribute = member.ToVpParameter();
                object             value     = member.GetDefaultValue();

                value = SanitizeValue(member.PropertyType, args.Count > attribute.Index
                    ? args[attribute.Index]
                    : value);

                member.SetValue(this, value, null);
            }
        }
Beispiel #9
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (property.propertyType == SerializedPropertyType.Integer)
        {
            position.height /= 2;

            FlagAttribute _Flag = attribute as FlagAttribute;

            EditorGUI.PropertyField(position, property, label);
            position.y += position.height;
            if (_Flag.itemStrs == null && _Flag.strName != "")
            {
                SerializedProperty _itemStrsProperty = property.serializedObject.FindProperty(_Flag.strName);
                if (_itemStrsProperty != null)
                {
                    _Flag.itemStrs = SerializedPropertyValue.GetArray <string>(_itemStrsProperty);
                }
                else
                {
                    EditorGUI.LabelField(position, label.text, "Property : \"" + _Flag.strName + "\" Not Found");
                    return;
                }
            }
            if (_Flag.itemStrs == null)
            {
                EditorGUI.LabelField(position, label.text, "Property : \"" + _Flag.strName + "\" Not String Array");
            }
            else
            {
                property.intValue = ExtendGUI.Flag(position, property.intValue, _Flag.itemStrs);
            }
        }
        else
        {
            EditorGUI.LabelField(position, label.text, "[Flag] Only for Int");
        }
    }
Beispiel #10
0
        private void RegisterFlag(string currentCommand, ArgumentProperty argumentProperty, FlagAttribute flag)
        {
            _flagNames.Add(flag.ShortName);
            _flagNames.Add(flag.LongName);

            _argumentProperties[currentCommand][flag.ShortName] = argumentProperty;
            _argumentProperties[currentCommand][flag.LongName]  = argumentProperty;
        }
Beispiel #11
0
        public ParametersList(MethodInfo methodInfo)
        {
            List <Parameter> args = new List <Parameter>();

            bool hasContextParameter = false;

            foreach (ParameterInfo param in methodInfo.GetParameters())
            {
                if (param.IsOptional)
                {
                    throw new UnsupportedOptionalParameterException(methodInfo, param);
                }

                bool isContext = false;
                if (param.ParameterType.IsAssignableFrom(typeof(ICommandContext)))
                {
                    hasContextParameter = true;
                    isContext           = true;
                }

                FlagAttribute flagAttribute = param.GetCustomAttribute <FlagAttribute>(false);
                NameAttribute nameAttribute = param.GetCustomAttribute <NameAttribute>(false);

                if (flagAttribute != null && param.ParameterType != typeof(bool))
                {
                    throw new UnsupportedNonBooleanFlagException(methodInfo, param);
                }

                Type checkType = param.ParameterType;
                if (param.ParameterType.IsArray || (param.ParameterType.IsGenericType && param.ParameterType.GetGenericTypeDefinition() == typeof(List <>)))
                {
                    checkType = param.ParameterType.GetElementType() ?? param.ParameterType.GetGenericArguments()[0];
                }

                if (checkType != typeof(ICommandContext))
                {
                    ITypeParser parser = Parser.GetParserForType(checkType);
                    if (parser == null)
                    {
                        throw new UnconvertableTypeParameterException(methodInfo, param);
                    }
                }

                if (flagAttribute == null && !isContext)
                {
                    RequiredParamterCount++;
                }

                args.Add(new Parameter(
                             name: nameAttribute?.Name,
                             parameterInfo: param,
                             isFlag: flagAttribute != null,
                             shortName: flagAttribute?.ShortName,
                             longName: flagAttribute?.LongName,
                             isParams: param.IsDefined(typeof(ParamArrayAttribute), false),
                             isContext: isContext,
                             defaultValue: flagAttribute?.DefaultValue ?? false));
            }

            // Throw exception if a ICommandContext parameter isnt found
            if (!hasContextParameter)
            {
                throw new MissingContextParameterException(methodInfo);
            }

            Parameters = args;
            Syntax     = GenerateSyntax();
        }