示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandLineParser" /> class.  See <see cref="CommandLineParser(Type, Type, CommandLineParserFlags)"/> 
 /// for full description of arguments.
 /// </summary>
 /// <param name="argumentSpecificationType">The <see cref="Type" /> from which the possible command line arguments should be retrieved.</param>
 /// <param name="flags">See <see cref="CommandLineParserFlags"/>.</param>
 /// <exception cref="ArgumentNullException"><paramref name="argumentSpecificationType" /> is a null reference.</exception>
 public CommandLineParser(Type argumentSpecificationType, CommandLineParserFlags flags)
     : this(argumentSpecificationType, null, flags)
 {
 }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandLineParser" /> class.  Provides a resource reader for localization of the command 
        /// line arguments.  String specified in the command line attributes are used to look up corresponding localized strings in the resources.
        /// Also provides flags to control parsing.
        /// </summary>
        /// <param name="argumentSpecificationType">The <see cref="Type" />  in which the command line arguments are defined.</param>
        /// <param name="resourceReaderType">A resource reader object with a <code>GetString</code> method</param>
        /// <param name="flags">See <see cref="CommandLineParserFlags"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="argumentSpecificationType" /> is a null reference.</exception>
        public CommandLineParser(Type argumentSpecificationType, Type resourceReaderType, CommandLineParserFlags flags)
        {
            if (argumentSpecificationType == null)
            {
                throw new ArgumentNullException("argumentSpecificationType");
            }

            this.argumentSpecificationType = argumentSpecificationType;
            this.resourceReaderType = resourceReaderType;
            this.flags= flags;
            this.responseFiles = new Stack<string>();

            argumentCollection = new List<CommandLineArgument>();

            // Look ONLY for public instance properties.  NOTE: Don't change this behavior!
            foreach (PropertyInfo propertyInfo in argumentSpecificationType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                object[] attributes = propertyInfo.GetCustomAttributes(typeof(CommandLineArgumentAttribute), true);
                CommandLineArgumentAttribute attribute;

                // Ignore properties that don't have a CommandLineArgumentAttribute
                if (attributes.Length == 1)
                    attribute = (CommandLineArgumentAttribute)attributes[0];
                else
                    continue;

                // Ensure that the property is readable and writeable
                if (!(propertyInfo.CanWrite && propertyInfo.CanRead))
                    throw new ArgumentException(
                        CommandLineParserResources.PropertyShouldBeReadableAndWriteable(propertyInfo.Name));

                attribute.ValueHint = ExternalGetString(attribute.ValueHint);

                if (attribute is DefaultCommandLineArgumentAttribute)
                {
                    if (HasDefaultArgument)
                        throw new ArgumentException(CommandLineParserResources.DefaultArgumentAlreadyDefined);

                    defaultArgument = new CommandLineArgument(attribute, propertyInfo);
                }
                else if (attribute is UnprocessedCommandLineArgumentAttribute)
                {
                    if (HasUnprocessedArgument)
                        throw new ArgumentException(CommandLineParserResources.UnprocessedArgumentAlreadyDefined);

                    unprocessedArgument = new CommandLineArgument(attribute, propertyInfo);

                    if (!unprocessedArgument.AllowMultiple)
                        throw new ArgumentException(CommandLineParserResources.UnprocessedArgumentMustBeArrayOrCollection);
                }
                else if (attribute is CommandCommandLineArgumentAttribute)
                {
                    if (HasCommandArgument)
                        throw new ArgumentException(CommandLineParserResources.CommandArgumentAlreadyDefined);

                    commandArgument = new CommandLineArgument(attribute, propertyInfo);
                }
                else
                {
                    attribute.Description = ExternalGetString(attribute.Description);

                    if (attribute.Description == null)
                    {
                        throw new ArgumentException(
                            CommandLineParserResources.PropertyDoesNotHaveAValueForDescription(propertyInfo.Name));
                    }

                    // If not being case sensitive, make everything lower case.
                    if (!CaseSensitive)
                    {
                        attribute.Name = attribute.Name.ToLower(CultureInfo.InvariantCulture);

                        if (attribute.ShortName != null)
                            attribute.ShortName = attribute.ShortName.ToLower(CultureInfo.InvariantCulture);
                    }

                    argumentCollection.Add(new CommandLineArgument(attribute, propertyInfo));
                }
            }

            if (HasUnprocessedArgument && !HasDefaultArgument)
            {
                throw new ArgumentException(CommandLineParserResources.UnprocessedRequiresDefaultArguments);
            }
        }