コード例 #1
0
        private void ProcessClassAttributes(Type type, string[] args)
        {
            CIHelpArgumentAttribute chta = type.GetCustomAttribute <CIHelpArgumentAttribute>();

            if (chta != null)
            {
                MustDisplayHelp = !args.All(name => !(chta.Name == name));
                ArgumentInfo ainfo = new ArgumentInfo(chta);
                argumentsInfo.Add(ainfo);
            }

            CICommandDescriptionAttribute cda = type.GetCustomAttribute <CICommandDescriptionAttribute>();

            if (cda != null)
            {
                CommandDescription = cda.Description;
            }

            MainJob = type.GetCustomAttribute <CIJobAttribute>();
        }
コード例 #2
0
        private void RegisterAttributesFromClassProperties(Type type, ArgumentInfoCollection argumentsInfo)
        {
            PropertyInfo[] properties          = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            int            simpleArgumentIndex = 0;

            foreach (PropertyInfo pinfo in properties)
            {
                ArgumentInfo ainfo             = null;
                CISimpleArgumentAttribute csaa = pinfo.GetCustomAttribute <CISimpleArgumentAttribute>();
                if (csaa == null)
                {
                    CINamedArgumentAttribute cnaa = pinfo.GetCustomAttribute <CINamedArgumentAttribute>();
                    if (cnaa == null)
                    {
                        continue; // Not a field associated with a console argument
                    }
                    if (cnaa.Names.Length == 0)
                    {
                        throw new InvalidArgumentDefinitionException(string.Format("No argument name associated with the property {0}.", pinfo.Name));
                    }

                    // If the collection already contains one the of arguments specified in the current AppArgumentAttribute
                    if (argumentsInfo.Contains(cnaa.Names))
                    {
                        throw new InvalidArgumentDefinitionException(string.Format("One of the argument specified with the property {0} has been already registered.", pinfo.Name));
                    }
                    ainfo = new ArgumentInfo(cnaa);
                }
                else
                {
                    ainfo = new ArgumentInfo(csaa, simpleArgumentIndex++);
                }

                ainfo.PInfo                    = pinfo;
                ainfo.MandatoryArgument        = pinfo.GetCustomAttribute <CIMandatoryArgumentAttribute>();
                ainfo.GroupedMandatoryArgument = pinfo.GetCustomAttribute <CIGroupedMandatoryArgumentAttribute>();
                ainfo.ArgumentBoundary         = pinfo.GetCustomAttribute <CIArgumentBoundaryAttribute>();
                ainfo.ArgumentValueLength      = pinfo.GetCustomAttribute <CIArgumentValueLengthAttribute>();
                ainfo.ArgumentFormat           = pinfo.GetCustomAttribute <CIArgumentFormatAttribute>();
                ainfo.Job         = pinfo.GetCustomAttribute <CIJobAttribute>();
                ainfo.FileContent = pinfo.GetCustomAttribute <CIFileContentAttribute>();
                ainfo.Password    = pinfo.GetCustomAttribute <CIPasswordAttribute>();
                ainfo.Exclusive   = pinfo.GetCustomAttribute <CIExclusiveArgumentAttribute>();

                if (pinfo.PropertyType.IsUserType())
                {
                    ainfo.UserType         = true;
                    ainfo.UserTypeInstance = Activator.CreateInstance(pinfo.PropertyType);

                    ArgumentInfoCollection childrenInfo = new ArgumentInfoCollection();
                    RegisterAttributesFromClassProperties(pinfo.PropertyType, childrenInfo);
                    ainfo.Children.AddRange(childrenInfo);
                }

                if (ainfo.NamedArgument != null &&
                    this.argumentsInfo.DeepContains(ainfo.Name))
                {
                    throw new DuplicateArgumentDefinitionException(ainfo.Name);
                }

                argumentsInfo.Add(ainfo);
            }
        }