internal static CommandLineAction Create(MethodInfo actionMethod, List <string> knownAliases) { var ret = PropertyInitializer.CreateInstance <CommandLineAction>(); ret.ActionMethod = actionMethod; ret.Source = actionMethod; ret.Aliases.Add(actionMethod.Name); ret.Metadata.AddRange(actionMethod.Attrs <IArgMetadata>().AssertAreAllInstanceOf <ICommandLineActionMetadata>()); ret.IgnoreCase = true; if (actionMethod.DeclaringType.HasAttr <ArgIgnoreCase>() && actionMethod.DeclaringType.Attr <ArgIgnoreCase>().IgnoreCase == false) { ret.IgnoreCase = false; } if (actionMethod.HasAttr <ArgIgnoreCase>() && actionMethod.Attr <ArgIgnoreCase>().IgnoreCase == false) { ret.IgnoreCase = false; } var directPipelineTarget = (from p in actionMethod.GetParameters() where p.HasAttr <ArgPipelineTarget>() select p).SingleOrDefault(); if (directPipelineTarget != null) { if (directPipelineTarget.Attr <ArgPipelineTarget>().PipelineOnly == false && ArgRevivers.CanRevive(directPipelineTarget.ParameterType) == false) { throw new InvalidArgDefinitionException("Method " + actionMethod.DeclaringType.FullName + "." + actionMethod.Name + " has parameter " + directPipelineTarget.Name + " of type " + directPipelineTarget.ParameterType.FullName + " which has set PipelineOnly to false, but has no reviver"); } ret.Arguments.AddRange(actionMethod.GetParameters().Where(p => CommandLineArgument.IsArgument(p)).Select(p => CommandLineArgument.Create(p))); foreach (var arg in (ret.Arguments).Where(a => a.Position >= 0)) { arg.Position++; // Since position 0 is reserved for the action specifier } } else { if (actionMethod.GetParameters().Length == 1 && ArgRevivers.CanRevive(actionMethod.GetParameters()[0].ParameterType) == false) { ret.Arguments.AddRange(actionMethod.GetParameters()[0].ParameterType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => CommandLineArgument.IsArgument(p)).Select(p => CommandLineArgument.Create(p, knownAliases))); } else if (actionMethod.GetParameters().Length > 0 && actionMethod.GetParameters().Where(p => ArgRevivers.CanRevive(p.ParameterType) == false).Count() == 0) { ret.Arguments.AddRange(actionMethod.GetParameters().Where(p => CommandLineArgument.IsArgument(p)).Select(p => CommandLineArgument.Create(p))); foreach (var arg in (ret.Arguments).Where(a => a.Position >= 0)) { arg.Position++; // Since position 0 is reserved for the action specifier } } else if (actionMethod.GetParameters().Length > 0) { throw new InvalidArgDefinitionException("Your action method contains a parameter that cannot be revived on its own. That is only valid if the non-revivable parameter is the only parameter. In that case, the properties of that parameter type will be used."); } } return(ret); }
private static List <CommandLineArgument> FindCommandLineArguments(Type t) { BindingFlags flags = BindingFlags.Instance | BindingFlags.Public; var knownAliases = new List <string>(); foreach (var prop in t.GetProperties(flags)) { // This makes sure that explicit aliases get put into the known aliases before any auto generated aliases knownAliases.AddRange(prop.Attrs <ArgShortcut>().Select(s => s.Shortcut)); } var ret = from p in t.GetProperties(flags) where CommandLineArgument.IsArgument(p) select CommandLineArgument.Create(p, knownAliases); return(ret.ToList()); }