private CommandLineReturnValues ContainsAllMandatoryParameter(CommandLineArgumentCollection args, object argsdef, out string MissingManadatoryParameters) { CommandLineReturnValues result; MissingManadatoryParameters = string.Empty; List <string> MissingManadatoryParameterList; result = ContainsAllMandatoryParameter(args, argsdef, out MissingManadatoryParameterList); if (result != 0) { return(result); } foreach (string s in MissingManadatoryParameterList) { MissingManadatoryParameters += s + ", "; } if (MissingManadatoryParameters.EndsWith(", ")) { MissingManadatoryParameters = MissingManadatoryParameters.Substring(0, MissingManadatoryParameters.Length - 2); } return(CommandLineReturnValues.OK); }
private int ContainsUnknownParameter(CommandLineArgumentCollection args, object argsdef, out string UnknownParameters) { int result = 0; UnknownParameters = string.Empty; List <string> unknownParametersList; result = ContainsUnknownParameter(args, argsdef, out unknownParametersList); if (result != 0) { return(result); } foreach (string s in unknownParametersList) { UnknownParameters += s + ", "; } if (UnknownParameters.EndsWith(", ")) { UnknownParameters = UnknownParameters.Substring(0, UnknownParameters.Length - 2); } return((int)CommandLineReturnValues.OK); }
private int ContainsUnknownParameter(CommandLineArgumentCollection args, object argsdef, out List <string> UnknownParameters) { UnknownParameters = new List <string>(); FieldInfo[] fic = argsdef.GetType().GetFields(); foreach (CommandLineArgument arg in args) { bool isUnknown = true; foreach (FieldInfo fi in fic) { if (fi.Name.ToLower() == arg.Name.ToLower()) { isUnknown = false; } } if (isUnknown) { UnknownParameters.Add(arg.Name); } } return((int)CommandLineReturnValues.OK); }
private CommandLineReturnValues SplitParameters(string[] args, out CommandLineArgumentCollection ArgumentCollection) { ArgumentCollection = new CommandLineArgumentCollection(); for (int i = 0; i <= (int)args.LongLength - 1; i++) { CommandLineArgument arg = new CommandLineArgument(); //check if the element starts with a '/' if (!args[i].StartsWith("/")) { return(CommandLineReturnValues.SynatxError); } //check for args like '/Mailbox:' that are normally strings or integers if (args[i].LastIndexOf(":") > 0) { arg.Name = args[i].Substring(1, args[i].IndexOf(":") - 1).ToLower(); if (arg.Name.Contains("password")) { arg.Value = args[i].Substring(args[i].IndexOf(":") + 1); } else { arg.Value = args[i].Substring(args[i].IndexOf(":") + 1).ToLower(); } } else //check for args like switches /doThis { arg.Name = args[i].Substring(1).ToLower(); arg.Value = "true"; } //add the argument to the collection try { ArgumentCollection.Add(arg); } catch (Exception ex) { Console.WriteLine("Could not add argument to collection. Error: {0}", ex.Message); return(CommandLineReturnValues.UndefinedError); } arg = null; } return(CommandLineReturnValues.OK); }
private CommandLineReturnValues ContainsAllMandatoryParameter(CommandLineArgumentCollection args, object argsdef, out List <string> MissingManadatoryParameters) { MissingManadatoryParameters = new List <string>(); FieldInfo[] fic = argsdef.GetType().GetFields(); foreach (FieldInfo fi in fic) { object[] customAttributes = fi.GetCustomAttributes(false); foreach (CommandLineArgument customAttribute in customAttributes) { if ((customAttribute.Type & ArgumentType.Required) == ArgumentType.Required) { if (fi.GetValue(argsdef) == null) { MissingManadatoryParameters.Add(fi.Name); } } } } return(0); }
private int ContainsAllMandatorySlaveParameter(CommandLineArgumentCollection args, object argsdef, out Dictionary <string, string> MissingManadatorySlaveParameters) { MissingManadatorySlaveParameters = new Dictionary <string, string>(); string[] masterArgumentValueArray; FieldInfo[] fic = argsdef.GetType().GetFields(); //for all the defined attributes in the user defined class foreach (FieldInfo fi in fic) { //get the custom attributes object[] customAttributes = fi.GetCustomAttributes(false); foreach (CommandLineArgument customAttribute in customAttributes) { //if the attribut is RequiredSlave and check if the MasterArgumentValue is present for the RequiredSlave argument if ( ((customAttribute.Type & ArgumentType.RequiredSlave) == ArgumentType.RequiredSlave) && (customAttribute.MasterArgument != null && customAttribute.MasterArgument != "")) { //go through the parameters and foreach (CommandLineArgument ca in args) { //get the list of master parameters string[] MasterParameterNames = customAttribute.MasterArgument.ToLower().Split(','); //for each master parameter defined for the parameter 'fi' for (int iMasterParameters = 0; iMasterParameters < MasterParameterNames.Length; iMasterParameters++) { //check if the master parameters is present if (ca.Name.ToLower() == MasterParameterNames[iMasterParameters]) { //verify if MasterArgumentValue is defined if (customAttribute.MasterArgumentValue == null) { //if not //check if there is some value at all given if (fi.GetValue(argsdef) == null) { //if not, try to add the parameter to the MissingManadatorySlaveParameters list try { MissingManadatorySlaveParameters.Add( MasterParameterNames[iMasterParameters] + "=*" + customAttribute.MasterArgumentValue, fi.Name); } catch { } } //if we have some value we can go to the next parameter break; } //verify if there are many values possible for MasterArgumentValue if (customAttribute.MasterArgumentValue.Contains(",")) { masterArgumentValueArray = customAttribute.MasterArgumentValue.Split(','); foreach (string s in masterArgumentValueArray) { if (argsdef.GetType().GetField(customAttribute.MasterArgument).GetValue(argsdef).ToString().ToLower() == s.ToLower()) { if (fi.GetValue(argsdef) == null) { //check if the paramater is already in the dictionary MissingManadatorySlaveParameters string temp; if (!MissingManadatorySlaveParameters.TryGetValue( customAttribute.MasterArgument + "=" + customAttribute.MasterArgumentValue, out temp)) { MissingManadatorySlaveParameters.Add( customAttribute.MasterArgument + "=" + customAttribute.MasterArgumentValue, fi.Name); } } } } } // if MasterArgumentValue is not defined else { if (argsdef.GetType().GetField(customAttribute.MasterArgument).GetValue(argsdef).ToString().ToLower() == customAttribute.MasterArgumentValue.ToLower()) { if (fi.GetValue(argsdef) == null) { MissingManadatorySlaveParameters.Add( customAttribute.MasterArgument + "=" + customAttribute.MasterArgumentValue, fi.Name); } } } } } } } } } return(0); }
private CommandLineReturnValues TransferParameterValues(CommandLineArgumentCollection args, object TargetObject) { foreach (CommandLineArgument arg in args) { FieldInfo[] fic = TargetObject.GetType().GetFields(); foreach (FieldInfo fi in fic) { if (fi.Name.ToLower() == arg.Name.ToLower()) { try { if (fi.FieldType.IsGenericType) { if (fi.FieldType.GetGenericArguments()[0].BaseType == typeof(Enum)) { fi.SetValue(parameterDefinitions, Enum.Parse(fi.FieldType.GetGenericArguments()[0], arg.Value, true)); } if (fi.FieldType == typeof(Nullable <int>)) { fi.SetValue(parameterDefinitions, Convert.ToInt32(arg.Value)); } if (fi.FieldType == typeof(Nullable <bool>)) { fi.SetValue(parameterDefinitions, Convert.ToBoolean(arg.Value)); } } else if (fi.FieldType == typeof(string)) { fi.SetValue(parameterDefinitions, arg.Value); } else if (fi.FieldType.BaseType == typeof(Array)) { if (fi.FieldType.GetElementType() == typeof(int)) { string[] a = arg.Value.Split(','); int[] values = Array.ConvertAll <string, int>(a, new Converter <string, int>(Convert.ToInt32)); fi.SetValue(TargetObject, values); } else if (fi.FieldType.GetElementType() == typeof(bool)) { throw new NotImplementedException(); //string[] a = arg.Value.Split(','); //int[] i = // Array.ConvertAll<string, int>(a, new Converter<string, int>(Convert.ToInt32)); //int[] values = // Array.ConvertAll<int, bool>(i, new Converter<int, bool>(Convert.ToBoolean)); //fi.SetValue(TargetObject, values); //string y = "1"; //bool x = Convert.ToBoolean(y); } else if (fi.FieldType.GetElementType() == typeof(string)) { string[] a = arg.Value.Split(','); fi.SetValue(TargetObject, a); } } else { fi.SetValue(TargetObject, arg.Value); } } catch (Exception ex) { Console.WriteLine("Could not convert the value for parameter '{0}' to '{1}'. The error was '{2}'", arg.Name, fi.FieldType.IsGenericType ? fi.FieldType.GetGenericArguments()[0].FullName : fi.FieldType.Name, ex.Message); return(CommandLineReturnValues.ConversionError); } } } } return(CommandLineReturnValues.OK); }