예제 #1
0
파일: Parser.cs 프로젝트: springtg/SvnTools
        /// <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 Parser(Type argumentSpecification, ErrorReporter reporter)
        {
            this.reporter    = reporter;
            this.arguments   = new ArrayList();
            this.argumentMap = new Hashtable();

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

            // add explicit names to map
            foreach (Argument argument in this.arguments)
            {
                Debug.Assert(!argumentMap.ContainsKey(argument.LongName));
                this.argumentMap[argument.LongName] = argument;
                if (argument.ExplicitShortName)
                {
                    if (argument.ShortName != null && argument.ShortName.Length > 0)
                    {
                        Debug.Assert(!argumentMap.ContainsKey(argument.ShortName));
                        this.argumentMap[argument.ShortName] = argument;
                    }
                    else
                    {
                        argument.ClearShortName();
                    }
                }
            }

            // add implicit names which don't collide to map
            foreach (Argument argument in this.arguments)
            {
                if (!argument.ExplicitShortName)
                {
                    if (argument.ShortName != null && argument.ShortName.Length > 0 && !argumentMap.ContainsKey(argument.ShortName))
                    {
                        this.argumentMap[argument.ShortName] = argument;
                    }
                    else
                    {
                        argument.ClearShortName();
                    }
                }
            }
        }
예제 #2
0
파일: Parser.cs 프로젝트: springtg/SvnTools
 private static string HelpText(ArgumentAttribute attribute, FieldInfo field)
 {
     if (attribute == null)
     {
         return(null);
     }
     else
     {
         return(attribute.HelpText);
     }
 }
예제 #3
0
파일: Parser.cs 프로젝트: springtg/SvnTools
 private static string ShortName(ArgumentAttribute attribute, FieldInfo field)
 {
     if (attribute is DefaultArgumentAttribute)
     {
         return(null);
     }
     if (!ExplicitShortName(attribute))
     {
         return(LongName(attribute, field).Substring(0, 1));
     }
     return(attribute.ShortName);
 }
예제 #4
0
파일: Parser.cs 프로젝트: springtg/SvnTools
 private static ArgumentType Flags(ArgumentAttribute attribute, FieldInfo field)
 {
     if (attribute != null)
     {
         return(attribute.Type);
     }
     else if (IsCollectionType(field.FieldType))
     {
         return(ArgumentType.MultipleUnique);
     }
     else
     {
         return(ArgumentType.AtMostOnce);
     }
 }
예제 #5
0
파일: Parser.cs 프로젝트: springtg/SvnTools
 private static object DefaultValue(ArgumentAttribute attribute, FieldInfo field)
 {
     return((attribute == null || !attribute.HasDefaultValue) ? null : attribute.DefaultValue);
 }
예제 #6
0
파일: Parser.cs 프로젝트: springtg/SvnTools
 private static bool ExplicitShortName(ArgumentAttribute attribute)
 {
     return(attribute != null && !attribute.DefaultShortName);
 }
예제 #7
0
파일: Parser.cs 프로젝트: springtg/SvnTools
 private static bool HasHelpText(ArgumentAttribute attribute)
 {
     return(attribute != null && attribute.HasHelpText);
 }
예제 #8
0
파일: Parser.cs 프로젝트: springtg/SvnTools
 private static string LongName(ArgumentAttribute attribute, FieldInfo field)
 {
     return((attribute == null || attribute.DefaultLongName) ? field.Name : attribute.LongName);
 }
예제 #9
0
			public Argument(ArgumentAttribute attribute, FieldInfo field, ErrorReporter reporter)
			{
				this.longName = Parser.LongName(attribute, field);
				this.explicitShortName = Parser.ExplicitShortName(attribute);
				this.shortName = Parser.ShortName(attribute, field);
				this.hasHelpText = Parser.HasHelpText(attribute);
				this.helpText = Parser.HelpText(attribute, field);
				this.defaultValue = Parser.DefaultValue(attribute, field);
				this.elementType = ElementType(field);
				this.flags = Flags(attribute, field);
				this.field = field;
				this.seenValue = false;
				this.reporter = reporter;
				this.isDefault = attribute != null && attribute is DefaultArgumentAttribute;
                this.isToggle = attribute.IsToggle;

				if (IsCollection)
				{
					this.collectionValues = new ArrayList();
				}
                
				Debug.Assert(this.longName != null && this.longName != "");
				Debug.Assert(!this.isDefault || !this.ExplicitShortName);
				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));
				Debug.Assert(!(this.IsRequired && this.HasDefaultValue), "Required arguments cannot have default value");
				Debug.Assert(!this.HasDefaultValue || (this.defaultValue.GetType() == field.FieldType), "Type of default value must match field type");
			}
예제 #10
0
		private static object DefaultValue(ArgumentAttribute attribute, FieldInfo field)
		{
			return (attribute == null || !attribute.HasDefaultValue) ? null : attribute.DefaultValue;
		}
예제 #11
0
		private static ArgumentType Flags(ArgumentAttribute attribute, FieldInfo field)
		{
			if (attribute != null)
				return attribute.Type;
			else if (IsCollectionType(field.FieldType))
				return ArgumentType.MultipleUnique;
			else
				return ArgumentType.AtMostOnce;
		}
예제 #12
0
		private static bool ExplicitShortName(ArgumentAttribute attribute)
		{
			return (attribute != null && !attribute.DefaultShortName);
		}
예제 #13
0
		private static bool HasHelpText(ArgumentAttribute attribute)
		{
			return (attribute != null && attribute.HasHelpText);
		}
예제 #14
0
		private static string HelpText(ArgumentAttribute attribute, FieldInfo field)
		{
			if (attribute == null)
				return null;
			else
				return attribute.HelpText;
		}
예제 #15
0
		private static string ShortName(ArgumentAttribute attribute, FieldInfo field)
		{
			if (attribute is DefaultArgumentAttribute)
				return null;
			if (!ExplicitShortName(attribute))
				return LongName(attribute, field).Substring(0,1);
			return attribute.ShortName;
		}
예제 #16
0
		private static string LongName(ArgumentAttribute attribute, FieldInfo field)
		{
			return (attribute == null || attribute.DefaultLongName) ? field.Name : attribute.LongName;
		}