コード例 #1
0
ファイル: SymbolVisitorAdapter.cs プロジェクト: qinezh/docfx
        private NamedArgumentInfo GetNamedArgumentInfo(KeyValuePair <string, TypedConstant> pair)
        {
            var result = new NamedArgumentInfo
            {
                Name = pair.Key,
            };
            var arg = pair.Value;

            if (arg.Type.TypeKind == TypeKind.Array)
            {
                // todo : value of array.
                return(null);
            }
            else if (arg.Value != null)
            {
                var type = arg.Value as ITypeSymbol;
                if (type != null)
                {
                    if (!FilterVisitor.CanVisitApi(type))
                    {
                        return(null);
                    }
                }
                result.Value = GetConstantValueForArgumentInfo(arg);
            }
            result.Type = AddSpecReference(arg.Type);
            return(result);
        }
コード例 #2
0
        public void TestArgumentInfoToString()
        {
            var namedArgumentInfo = new NamedArgumentInfo()
            {
                LongName = "dll"
            };

            Assert.That(namedArgumentInfo.ToString(), Is.EqualTo("--dll"));

            namedArgumentInfo.ShortName = 'd';
            Assert.That(namedArgumentInfo.ToString(), Is.EqualTo("--dll [-d]"));
        }
コード例 #3
0
        /// <summary>
        /// Creates a parser instance and configures the parser for the properties in the given class T.
        /// In addition, action methods are created, which support the method ParseIntoObject
        /// </summary>
        /// <param name="args">Arguments to be parsed</param>
        /// <param name="filters">Filters to be added to the parser. May be null, to add no additional
        /// filters</param>
        /// <returns>The created and preconfigured parser</returns>
        public Parser PrepareParser(string[] args, IEnumerable <ICommandLineFilter> filters = null)
        {
            this.parser  = new Parser(args);
            this.actions = new List <ParseAction>();

            var type = typeof(T);

            if (type.IsPrimitive)
            {
                throw new InvalidOperationException("Parsing into primitives is not supported");
            }

            // Go through the properties
            foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                // Gets the attribute
                var attributeNamedArgument   = property.GetCustomAttribute(typeof(NamedArgumentAttribute)) as NamedArgumentAttribute;
                var attributeUnnamedArgument = property.GetCustomAttribute(typeof(UnnamedArgumentAttribute)) as UnnamedArgumentAttribute;
                var attributeArgument        =
                    attributeNamedArgument == null ?
                    attributeUnnamedArgument as ArgumentInfoAttribute :
                    attributeNamedArgument as ArgumentInfoAttribute;

                // Per default, a named argument info is created
                ArgumentInfo argumentInfo;
                if (attributeUnnamedArgument != null)
                {
                    argumentInfo = new UnnamedArgumentInfo();
                }
                else
                {
                    argumentInfo = new NamedArgumentInfo();
                }

                // Defines the getter to retrieve the data
                Func <string> getter;

                if (argumentInfo is NamedArgumentInfo)
                {
                    var namedArgumentInfo = argumentInfo as NamedArgumentInfo;
                    namedArgumentInfo.LongName = property.Name;

                    if (attributeNamedArgument != null)
                    {
                        if (!string.IsNullOrEmpty(attributeNamedArgument.LongName))
                        {
                            namedArgumentInfo.LongName = attributeNamedArgument.LongName;
                        }

                        if (attributeNamedArgument.ShortName != '\0')
                        {
                            namedArgumentInfo.ShortName = attributeNamedArgument.ShortName;
                        }
                    }

                    getter = () =>
                    {
                        string result;

                        if (parser.NamedArguments.TryGetValue(namedArgumentInfo.LongName, out result))
                        {
                            return(result);
                        }
                        else
                        {
                            return(null);
                        }
                    };
                }
                else if (argumentInfo is UnnamedArgumentInfo)
                {
                    var unnamedArgumentInfo = argumentInfo as UnnamedArgumentInfo;
                    if (attributeUnnamedArgument != null)
                    {
                        if (attributeUnnamedArgument.Index != 0)
                        {
                            unnamedArgumentInfo.Index = attributeUnnamedArgument.Index;
                        }
                        else
                        {
                            // Auto increase, if an unnamed argument already exists and
                            // no index has been set
                            unnamedArgumentInfo.Index = parser.UnnamedArgumentInfos.Count();
                        }
                    }

                    getter = () =>
                    {
                        if (parser.UnnamedArguments.Count > unnamedArgumentInfo.Index)
                        {
                            return(parser.UnnamedArguments[unnamedArgumentInfo.Index]);
                        }
                        else
                        {
                            return(null);
                        }
                    };
                }
                else
                {
                    throw new NotImplementedException("Unexpected argument info type: " + argumentInfo.GetType().ToString());
                }

                // Parses the rest of the attribute
                if (attributeArgument != null)
                {
                    if (!string.IsNullOrEmpty(attributeArgument.HelpText))
                    {
                        argumentInfo.HelpText = attributeArgument.HelpText;
                    }

                    if (!string.IsNullOrEmpty(attributeArgument.DefaultValue))
                    {
                        argumentInfo.DefaultValue = attributeArgument.DefaultValue;
                    }

                    argumentInfo.IsRequired = attributeArgument.IsRequired;
                }

                this.HandlePropertyType(property, getter, argumentInfo);

                parser.AddArgumentInfo(argumentInfo);
            }

            return(this.parser);
        }