/// <summary> /// Constructs and prints out a help string for all <see cref="CommandLineCommand"/> classes supported by this program /// </summary> /// <param name="manipulator">Unused parameter</param> public override void PerformFunction(MySqlDataManipulator manipulator) { var commands = ReflectionHelper.GetAllCommands(); string format = "Command{0}:\n\tKeyed Arguments: {1}\n\tPositional Arguments: {2}\n"; int index = 1; foreach (CommandLineMapping mapping in commands) { //Determine all keyed and positional arguments required by the current command //these arguments are determined by the attributes applied to the fields of the current command List <string> keyedArguments = new List <string>(); List <string> positionalArguments = new List <string>(); foreach (FieldInfo f in mapping.KeyedFields) { KeyedArgument arg = (KeyedArgument)System.Attribute.GetCustomAttribute(f, typeof(KeyedArgument)); string toAdd = null; if (arg.ValueRequired) { toAdd = arg.Key + ' ' + arg.RequiredValue; } else { toAdd = arg.Key + ' ' + "%" + f.Name + "%: " + f.FieldType.Name; } keyedArguments.Add(toAdd); } foreach (FieldInfo f in mapping.PositionalFields) { PositionalArgument arg = (PositionalArgument)System.Attribute.GetCustomAttribute(f, typeof(PositionalArgument)); while (arg.Position >= positionalArguments.Count) { positionalArguments.Add(null); } positionalArguments[arg.Position] = "\"%" + f.Name + "%\": " + f.FieldType.Name; } //Display the formatted help string string toPrint = string.Format(format, index, string.Join(' ', keyedArguments), string.Join(' ', positionalArguments)); Console.WriteLine(toPrint); index++; } }
/// <summary> /// Analyzes the command lines stored in <paramref name="argumentsIn"/> and performs any company or user creation necessary /// </summary> /// <param name="manipulatorIn">Object to use to access the database if needed</param> /// <param name="argumentsIn">The command line arguments to analyze</param> /// <returns>True if there was a creation request made (thus the program should terminate), false otherwise (the program should continue onward)</returns> public static bool PerformRequestedCreation(MySqlDataManipulator manipulatorIn, CommandLineArgumentParser argumentsIn) { var commands = ReflectionHelper.GetAllCommands(); foreach (CommandLineMapping mapping in commands) { if (mapping.KeyedFields.Count != argumentsIn.KeyedArguments.Count) { continue; } if (mapping.PositionalFields.Count != argumentsIn.PositionalArguments.Count) { continue; } bool keysPresent = true; foreach (FieldInfo f in mapping.KeyedFields) { KeyedArgument arg = (KeyedArgument)System.Attribute.GetCustomAttribute(f, typeof(KeyedArgument)); if (!argumentsIn.KeyedArguments.ContainsKey(arg.Key)) { keysPresent = false; break; } if (arg.ValueRequired && !argumentsIn.KeyedArguments[arg.Key].Equals(arg.RequiredValue)) { keysPresent = false; break; } object value = null; if (f.FieldType.IsPrimitive) { value = f.FieldType.GetMethod("Parse", new[] { typeof(string) }).Invoke(null, new[] { argumentsIn.KeyedArguments[arg.Key] }); } else if (f.FieldType.Equals(typeof(string))) { value = argumentsIn.KeyedArguments[arg.Key]; } else { throw new ArgumentException("Non primitive field marked as a Keyed Argument in class " + mapping.Command.GetType()); } f.SetValue(mapping.Command, value); } if (!keysPresent) { continue; } keysPresent = true; foreach (FieldInfo f in mapping.PositionalFields) { PositionalArgument arg = (PositionalArgument)System.Attribute.GetCustomAttribute(f, typeof(PositionalArgument)); if (arg.Position > mapping.PositionalFields.Count) { keysPresent = false; break; } object value = null; if (f.FieldType.IsPrimitive) { value = f.FieldType.GetMethod("Parse", new[] { typeof(string) }).Invoke(null, new[] { argumentsIn.PositionalArguments[arg.Position] }); } else if (f.FieldType.Equals(typeof(string))) { value = argumentsIn.PositionalArguments[arg.Position]; } else { throw new ArgumentException("Non primitive, Non string field marked as a Positional Argument in class " + mapping.Command.GetType()); } f.SetValue(mapping.Command, value); } if (!keysPresent) { continue; } mapping.Command.PerformFunction(manipulatorIn); return(true); } return(false); }