internal static bool MatchesSpecifiedArg(this PropertyInfo prop, string specifiedArg) { bool ignoreCase = true; if (prop.HasAttr <ArgIgnoreCase>() && !prop.Attr <ArgIgnoreCase>().IgnoreCase) { ignoreCase = false; } else if (prop.DeclaringType.HasAttr <ArgIgnoreCase>() && !prop.DeclaringType.Attr <ArgIgnoreCase>().IgnoreCase) { ignoreCase = false; } var shortcut = ArgShortcut.GetShortcut(prop); if (ignoreCase && shortcut != null) { return(prop.Name.ToLower() == specifiedArg.ToLower() || shortcut.ToLower() == specifiedArg.ToLower()); } else if (ignoreCase) { return(prop.Name.ToLower() == specifiedArg.ToLower()); } else { return(prop.Name == specifiedArg || shortcut == specifiedArg); } }
/// <summary> /// Generate a new info instance given a reflected property. /// </summary> /// <param name="toAutoGen">The property to use to seed the usage info</param> public ArgumentUsageInfo(PropertyInfo toAutoGen) : this() { Property = toAutoGen; Ignore = toAutoGen.HasAttr <ArgIgnoreAttribute>(); IsAction = toAutoGen.IsActionArgProperty(); IsActionArgs = toAutoGen.Name == Constants.ActionPropertyConventionName; Name = toAutoGen.GetArgumentName(); IsRequired = toAutoGen.HasAttr <ArgRequired>(); if (ArgShortcut.GetShortcut(toAutoGen) != null) { Aliases.Add("-" + ArgShortcut.GetShortcut(toAutoGen)); } Type = toAutoGen.PropertyType.Name; if (KnownTypeMappings.ContainsKey(Type)) { Type = KnownTypeMappings[Type]; } else { Type = Type.ToLower(); } Position = toAutoGen.HasAttr <ArgPosition>() ? new int?(toAutoGen.Attr <ArgPosition>().Position) : null; Description = ""; if (toAutoGen.HasAttr <ArgDescription>()) { Description = toAutoGen.Attr <ArgDescription>().Description; } }
private void ValidateArgScaffold(Type t, List <string> shortcuts = null, Type parentType = null) { if (parentType != null) { if (parentType.HasAttr <ArgIgnoreCase>() ^ t.HasAttr <ArgIgnoreCase>()) { throw new InvalidArgDefinitionException("If you specify the " + typeof(ArgIgnoreCase).Name + " attribute on your base type then you must also specify it on each action type."); } else if (parentType.HasAttr <ArgIgnoreCase>() && parentType.Attr <ArgIgnoreCase>().IgnoreCase != t.Attr <ArgIgnoreCase>().IgnoreCase) { throw new InvalidArgDefinitionException("If you specify the " + typeof(ArgIgnoreCase).Name + " attribute on your base and acton types then they must be configured to use the same value for IgnoreCase."); } } if (t.Attrs <ArgIgnoreCase>().Count > 1) { throw new InvalidArgDefinitionException("An attribute that is or derives from " + typeof(ArgIgnoreCase).Name + " was specified on your type more than once"); } var actionProp = ArgAction.GetActionProperty(t); shortcuts = shortcuts ?? new List <string>(); bool ignoreCase = true; if (t.HasAttr <ArgIgnoreCase>() && t.Attr <ArgIgnoreCase>().IgnoreCase == false) { ignoreCase = false; } foreach (PropertyInfo prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (prop.Attr <ArgIgnoreAttribute>() != null) { continue; } if (prop.IsActionArgProperty() && actionProp != null) { continue; } if (ArgRevivers.CanRevive(prop.PropertyType) == false) { throw new InvalidArgDefinitionException("There is no reviver for type " + prop.PropertyType.Name + ". Offending Property: " + prop.DeclaringType.Name + "." + prop.GetArgumentName()); } var shortcut = ArgShortcut.GetShortcut(prop); if (ignoreCase && shortcut != null) { shortcut = shortcut.ToLower(); } if (shortcut != null && shortcuts.Contains(shortcut)) { throw new InvalidArgDefinitionException("Duplicate arg options with shortcut '" + ArgShortcut.GetShortcut(prop) + "'. Keep in mind that shortcuts are not case sensitive unless you use the [ArgIgnoreCase(false)] attribute. For example, Without this attribute the shortcuts '-a' and '-A' would cause this exception."); } else if (shortcut != null) { shortcuts.Add(shortcut); } } if (actionProp != null) { foreach (PropertyInfo prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (prop.IsActionArgProperty()) { ArgAction.ResolveMethod(t, prop); ValidateArgScaffold(prop.PropertyType, shortcuts.ToArray().ToList(), t); } } } }