public static ArgumentCollection Parse(string[] args) { ArgumentCollection collection = new ArgumentCollection(); foreach (string nextArg in args) { string currentArg = StripArgumentIdentifyer(nextArg); string argName = String.Empty; object argValue = null; if (currentArg.Contains("=")) { int indexOfEqualsSign = currentArg.IndexOf("="); argName = currentArg.Substring(0, indexOfEqualsSign); if (__map.ContainsKey(argName)) { if (__map[argName] == ArgumentType.Null) { argValue = null; } else if (__map[argName] == ArgumentType.String) { argValue = currentArg.Substring(indexOfEqualsSign + 1); } else if (__map[argName] == ArgumentType.StringArray) { argValue = currentArg.Substring(indexOfEqualsSign + 1).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); } else if (__map[argName] == ArgumentType.Int) { int outValue = 0; if (Int32.TryParse(currentArg.Substring(indexOfEqualsSign + 1), out outValue)) { argValue = outValue; } else { throw new ArgumentException(String.Format("Argument {0} must be of int (Int32) type", argName)); } } else if (__map[argName] == ArgumentType.IntOrString) { int outValue = 0; if (Int32.TryParse(currentArg.Substring(indexOfEqualsSign + 1), out outValue)) { argValue = outValue; } else { argValue = currentArg.Substring(indexOfEqualsSign + 1); } } } else { throw new ArgumentException(String.Format("{0} is not recognized as a valid argument", argName)); } } else { argName = currentArg; } collection.Add(argName, argValue); } return(collection); }
private Program(string[] args) { _arguments = ArgumentCollection.Parse(args); Run(); }