/// <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; } }
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); } }
internal static void RegisterShortcuts(Type t, List <string> shortcuts = null) { RegisteredTypes.Add(t); bool isNested = shortcuts != null; shortcuts = isNested ? shortcuts : new List <string>(); var actionProp = ArgAction.GetActionProperty(t); foreach (PropertyInfo prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (prop.Attr <ArgIgnoreAttribute>() != null) { continue; } if (prop.IsActionArgProperty() && actionProp != null) { continue; } var shortcut = ArgShortcut.GetShortcutInternal(prop, shortcuts); if (shortcut != null) { shortcuts.Add(shortcut); if (KnownShortcuts.ContainsKey(prop) == false) { KnownShortcuts.Add(prop, shortcut); } else { KnownShortcuts[prop] = shortcut; } } } if (actionProp != null) { foreach (PropertyInfo prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (prop.IsActionArgProperty()) { RegisterShortcuts(prop.PropertyType, shortcuts); } } } }
private ArgAction ParseInternal(Type t, string[] input) { ArgShortcut.RegisterShortcuts(t); ValidateArgScaffold(t); var context = new ArgHook.HookContext(); context.Args = Activator.CreateInstance(t); context.CmdLineArgs = input; t.RunBeforeParse(context); var specifiedActionProperty = FindSpecifiedAction(t, ref context.CmdLineArgs); context.ParserData = ArgParser.Parse(context.CmdLineArgs); PopulateProperties(context); if (specifiedActionProperty != null) { var actionPropertyValue = Activator.CreateInstance(specifiedActionProperty.PropertyType); var toRestore = context.Args; context.Args = actionPropertyValue; PopulateProperties(context); context.Args = toRestore; specifiedActionProperty.SetValue(context.Args, actionPropertyValue, null); } if (context.ParserData.ImplicitParameters.Count > 0) { throw new UnexpectedArgException("Unexpected unnamed argument: " + context.ParserData.ImplicitParameters.First().Value); } if (context.ParserData.ExplicitParameters.Count > 0) { throw new UnexpectedArgException("Unexpected named argument: " + context.ParserData.ExplicitParameters.First().Key); } return(new ArgAction() { Value = context.Args, ActionArgs = specifiedActionProperty != null?specifiedActionProperty.GetValue(context.Args, null) : null, ActionArgsProperty = specifiedActionProperty }); }
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); } } } }