示例#1
0
            public Argument(CommandLineArgumentAttribute attribute, FieldInfo field, ErrorReporter reporter)
            {
                LongName          = CommandLineArgumentParser.LongName(attribute, field);
                ExplicitShortName = CommandLineArgumentParser.ExplicitShortName(attribute);
                ShortName         = CommandLineArgumentParser.ShortName(attribute, field);
                elementType       = ElementType(field);
                flags             = Flags(attribute, field);
                this.field        = field;
                SeenValue         = false;
                this.reporter     = reporter;
                IsDefault         = attribute != null && attribute is DefaultCommandLineArgumentAttribute;

                if (IsCollection)
                {
                    collectionValues = new ArrayList();
                }

                Debug.Assert(LongName != null && LongName.Length > 0);
                Debug.Assert(!IsCollection || AllowMultiple, "Collection arguments must have allow multiple");
                Debug.Assert(!Unique || IsCollection, "Unique only applicable to collection arguments");
                Debug.Assert(IsValidElementType(Type) ||
                             IsCollectionType(Type));
                Debug.Assert((IsCollection && IsValidElementType(elementType)) ||
                             (!IsCollection && elementType == null));
            }
示例#2
0
        /// <summary>
        /// Creates a new command line argument parser.
        /// </summary>
        /// <param name="argumentSpecification"> The type of object to  parse. </param>
        /// <param name="reporter"> The destination for parse errors. </param>
        public CommandLineArgumentParser(Type argumentSpecification, ErrorReporter reporter)
        {
            this.reporter = reporter;
            arguments     = new ArrayList();
            argumentMap   = new Hashtable();

            foreach (FieldInfo field in argumentSpecification.GetFields())
            {
                if (!field.IsStatic && !field.IsInitOnly && !field.IsLiteral)
                {
                    CommandLineArgumentAttribute attribute = GetAttribute(field);
                    if (attribute is DefaultCommandLineArgumentAttribute)
                    {
                        Debug.Assert(defaultArgument == null);
                        defaultArgument = new Argument(attribute, field, reporter);
                    }
                    else
                    {
                        arguments.Add(new Argument(attribute, field, reporter));
                    }
                }
            }

            // add explicit names to map
            foreach (Argument argument in this.arguments)
            {
                Debug.Assert(!argumentMap.ContainsKey(argument.LongName.ToLower(CultureInfo.InvariantCulture)));

                argumentMap[argument.LongName.ToLower(CultureInfo.InvariantCulture)] = argument;

                if (argument.ExplicitShortName && argument.ShortName != null && argument.ShortName.Length > 0)
                {
                    Debug.Assert(!argumentMap.ContainsKey(argument.ShortName.ToLower(CultureInfo.InvariantCulture)));

                    argumentMap[argument.ShortName.ToLower(CultureInfo.InvariantCulture)] = argument;
                }
            }

            // add implicit names which don't collide to map
            foreach (Argument argument in this.arguments)
            {
                if (!argument.ExplicitShortName && argument.ShortName != null && argument.ShortName.Length > 0)
                {
                    if (!argumentMap.ContainsKey(argument.ShortName.ToLower(CultureInfo.InvariantCulture)))
                    {
                        argumentMap[argument.ShortName.ToLower(CultureInfo.InvariantCulture)] = argument;
                    }
                }
            }
        }
示例#3
0
 private static CommandLineArgumentType Flags(CommandLineArgumentAttribute attribute, FieldInfo field)
 {
     if (attribute != null)
     {
         return(attribute.Type);
     }
     else if (IsCollectionType(field.FieldType))
     {
         return(CommandLineArgumentType.MultipleUnique);
     }
     else
     {
         return(CommandLineArgumentType.AtMostOnce);
     }
 }
示例#4
0
 private static bool ExplicitShortName(CommandLineArgumentAttribute attribute)
 {
     return(attribute != null && !attribute.DefaultShortName);
 }
示例#5
0
 private static string ShortName(CommandLineArgumentAttribute attribute, FieldInfo field)
 {
     return(!ExplicitShortName(attribute) ? LongName(attribute, field).Substring(0, 1) : attribute.ShortName);
 }
示例#6
0
 private static string LongName(CommandLineArgumentAttribute attribute, FieldInfo field)
 {
     return((attribute == null || attribute.DefaultLongName) ? field.Name : attribute.LongName);
 }