Exemplo n.º 1
0
        /// <summary>
        ///     Retrieves a collection of <see cref="ArgumentInfo"/> gathered from properties in the target <paramref name="type"/>
        ///     marked with the <see cref="ArgumentAttribute"/><see cref="Attribute"/> along with the short and long names and help text.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> for which the matching properties are to be retrieived.</param>
        /// <param name="caller">Internal parameter used to identify the calling method.</param>
        /// <returns>The retrieved collection of <see cref="ArgumentInfo"/>.</returns>
        public static IEnumerable <ArgumentInfo> GetArgumentInfo(Type type = null, [CallerMemberName] string caller = default)
        {
            type ??= ArgumentsExtensions.GetCallingType(caller);
            var retVal = new List <ArgumentInfo>();

            foreach (PropertyInfo property in GetArgumentProperties(type).Values.Distinct())
            {
                CustomAttributeData attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == typeof(ArgumentAttribute).Name);

                if (attribute != default(CustomAttributeData))
                {
                    retVal.Add(new ArgumentInfo(
                                   shortName: (char)attribute.ConstructorArguments[0].Value,
                                   longName: (string)attribute.ConstructorArguments[1].Value,
                                   helpText: (string)attribute.ConstructorArguments[2].Value,
                                   property: property));
                }
            }

            return(retVal);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Populates the properties in the invoking class marked with the
        ///     <see cref="ArgumentAttribute"/><see cref="Attribute"/> with the values specified in the list of command line
        ///     arguments, if present.
        /// </summary>
        /// <param name="commandLineString">The command line arguments with which the application was started.</param>
        /// <param name="clearExistingValues">Whether to clear the properties before populating them. Defaults to true.</param>
        /// <param name="caller">Internal parameter used to identify the calling method.</param>
        public static void Populate(string commandLineString = default, bool clearExistingValues = true, [CallerMemberName] string caller = default)
        {
            var type = ArgumentsExtensions.GetCallingType(caller);

            Populate(type, Parse(commandLineString, options => options.TargetType = type), clearExistingValues);
        }