/// <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 CommandLineParser(Type argumentSpecification, ErrorReporter reporter) { _reporter = reporter; _arguments = new ArrayList(); _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(_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 _arguments) { Debug.Assert(!_argumentMap.ContainsKey(argument.LongName)); _argumentMap[argument.LongName] = argument; if (argument.ExplicitShortName) { if (!string.IsNullOrEmpty(argument.ShortName)) { Debug.Assert(!_argumentMap.ContainsKey(argument.ShortName)); _argumentMap[argument.ShortName] = argument; } else { argument.ClearShortName(); } } } // add implicit names which don't collide to map foreach (Argument argument in _arguments) { if (!argument.ExplicitShortName) { if (!string.IsNullOrEmpty(argument.ShortName) && !_argumentMap.ContainsKey(argument.ShortName)) { _argumentMap[argument.ShortName] = argument; } else { argument.ClearShortName(); } } } }
private static string HelpText(ArgumentAttribute attribute, FieldInfo field) { if (attribute == null) { return(null); } return(attribute.HelpText); }
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); }
private static ArgumentType Flags(ArgumentAttribute attribute, FieldInfo field) { if (attribute != null) { return(attribute.Type); } if (IsCollectionType(field.FieldType)) { return(ArgumentType.MultipleUnique); } return(ArgumentType.AtMostOnce); }
public Argument(ArgumentAttribute attribute, FieldInfo field, ErrorReporter reporter) { _longName = CommandLineParser.LongName(attribute, field); _explicitShortName = CommandLineParser.ExplicitShortName(attribute); _shortName = CommandLineParser.ShortName(attribute, field); _hasHelpText = CommandLineParser.HasHelpText(attribute); _helpText = CommandLineParser.HelpText(attribute, field); _defaultValue = CommandLineParser.DefaultValue(attribute, field); _elementType = ElementType(field); _flags = Flags(attribute, field); _field = field; _seenValue = false; _reporter = reporter; _isDefault = attribute is DefaultArgumentAttribute; if (IsCollection) { _collectionValues = new ArrayList(); } Debug.Assert(!string.IsNullOrEmpty(_longName)); Debug.Assert(!_isDefault || !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(!(IsRequired && HasDefaultValue), "Required arguments cannot have default value"); Debug.Assert(!HasDefaultValue || (_defaultValue.GetType() == field.FieldType), "Type of default value must match field type"); }
private static ArgumentType Flags(ArgumentAttribute attribute, FieldInfo field) { if (attribute != null) { return attribute.Type; } if (IsCollectionType(field.FieldType)) { return ArgumentType.MultipleUnique; } return ArgumentType.AtMostOnce; }
private static object DefaultValue(ArgumentAttribute attribute, FieldInfo field) { return (attribute == null || !attribute.HasDefaultValue) ? null : attribute.DefaultValue; }
private static bool ExplicitShortName(ArgumentAttribute attribute) { return (attribute != null && !attribute.DefaultShortName); }
private static bool HasHelpText(ArgumentAttribute attribute) { return (attribute != null && attribute.HasHelpText); }
private static string HelpText(ArgumentAttribute attribute, FieldInfo field) { if (attribute == null) { return null; } return attribute.HelpText; }
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; }
private static string LongName(ArgumentAttribute attribute, FieldInfo field) { return (attribute == null || attribute.DefaultLongName) ? field.Name : attribute.LongName; }
private static object DefaultValue(ArgumentAttribute attribute, FieldInfo field) { return((attribute == null || !attribute.HasDefaultValue) ? null : attribute.DefaultValue); }
private static bool ExplicitShortName(ArgumentAttribute attribute) { return(attribute != null && !attribute.DefaultShortName); }
private static bool HasHelpText(ArgumentAttribute attribute) { return(attribute != null && attribute.HasHelpText); }
private static string LongName(ArgumentAttribute attribute, FieldInfo field) { return((attribute == null || attribute.DefaultLongName) ? field.Name : attribute.LongName); }