internal static CommandLineAction Create(PropertyInfo actionProperty, List <string> knownAliases) { var ret = PropertyInitializer.CreateInstance <CommandLineAction>(); ret.ActionMethod = ArgAction.ResolveMethod(actionProperty.DeclaringType, actionProperty); ret.Source = actionProperty; ret.Arguments.AddRange(new CommandLineArgumentsDefinition(actionProperty.PropertyType).Arguments); ret.IgnoreCase = true; if (actionProperty.DeclaringType.HasAttr <ArgIgnoreCase>() && actionProperty.DeclaringType.Attr <ArgIgnoreCase>().IgnoreCase == false) { ret.IgnoreCase = false; } if (actionProperty.HasAttr <ArgIgnoreCase>() && actionProperty.Attr <ArgIgnoreCase>().IgnoreCase == false) { ret.IgnoreCase = false; } ret.Metadata.AddRange(actionProperty.Attrs <IArgMetadata>().AssertAreAllInstanceOf <ICommandLineActionMetadata>()); // This line only calls into CommandLineArgument because the code to strip 'Args' off the end of the // action property name lives here. This is a pre 2.0 hack that's only left in place to support apps that // use the 'Args' suffix pattern. ret.Aliases.AddRange(CommandLineArgument.FindDefaultShortcuts(actionProperty, knownAliases, ret.IgnoreCase)); return(ret); }
internal static CommandLineArgument Create(PropertyInfo property, List <string> knownAliases) { var ret = PropertyInitializer.CreateInstance <CommandLineArgument>(); ret.DefaultValue = property.HasAttr <DefaultValueAttribute>() ? property.Attr <DefaultValueAttribute>().Value : null; ret.Position = property.HasAttr <ArgPosition>() ? property.Attr <ArgPosition>().Position : -1; ret.Source = property; ret.ArgumentType = property.PropertyType; ret.IgnoreCase = true; if (property.DeclaringType.HasAttr <ArgIgnoreCase>() && property.DeclaringType.Attr <ArgIgnoreCase>().IgnoreCase == false) { ret.IgnoreCase = false; } if (property.HasAttr <ArgIgnoreCase>() && property.Attr <ArgIgnoreCase>().IgnoreCase == false) { ret.IgnoreCase = false; } ret.Aliases.AddRange(FindDefaultShortcuts(property, knownAliases, ret.IgnoreCase)); // TODO - I think the first generic call can just be more specific ret.Metadata.AddRange(property.Attrs <IArgMetadata>().AssertAreAllInstanceOf <ICommandLineArgumentMetadata>()); return(ret); }
internal static CommandLineArgument Create(ParameterInfo parameter) { var ret = PropertyInitializer.CreateInstance <CommandLineArgument>(); ret.Position = parameter.Position; ret.ArgumentType = parameter.ParameterType; ret.Source = parameter; ret.DefaultValue = parameter.HasAttr <DefaultValueAttribute>() ? parameter.Attr <DefaultValueAttribute>().Value : null; ret.IgnoreCase = true; if (parameter.Member.DeclaringType.HasAttr <ArgIgnoreCase>() && parameter.Member.DeclaringType.Attr <ArgIgnoreCase>().IgnoreCase == false) { ret.IgnoreCase = false; } if (parameter.HasAttr <ArgIgnoreCase>() && parameter.Attr <ArgIgnoreCase>().IgnoreCase == false) { ret.IgnoreCase = false; } ret.Aliases.Add(parameter.Name); ret.Metadata.AddRange(parameter.Attrs <IArgMetadata>().AssertAreAllInstanceOf <ICommandLineArgumentMetadata>()); return(ret); }
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); }