/// <summary>Processes a set of command line arguments.</summary> /// <param name="args"> /// Command line arguments to process. This is usally coming from your Main method. /// </param> /// <param name="outErrors"> /// Optional "out" parameter that holds error strings for every encountered error. /// </param> /// <returns> /// <c>true</c> if the arguments in <paramref name="args"/> are valid; otherwise /// <c>false</c> . /// </returns> public bool Validate(string[] args, OptionalOut <string[]> outErrors = null) { string[] new_args = new string[args.Length]; for (int i = 0; i < args.Length; i++) { new_args[i] = args[i].Replace("&", "&").Replace(" ", "&sp;").Replace(":\\", "&col;\\"); } return(Validate(string.Join(" ", new_args), outErrors)); }
/// <summary>Processes a set of command line arguments.</summary> /// <param name="args"> /// Command line arguments to process. This is usally coming from your Main method. /// </param> /// <param name="outErrors"> /// Optional "out" parameter that holds error strings for every encountered error. /// </param> /// <returns> /// <c>true</c> if the arguments in <paramref name="args"/> are valid; otherwise /// <c>false</c> . /// </returns> public bool Validate(string args, OptionalOut <string[]> outErrors = null) { Reset(); bool ignoreAlreadyHandled = false; if (DefaultArgument != null) { ignoreAlreadyHandled = Arguments[DefaultArgument].SupportsMultipleValues; } bool handledDefault = false; bool errors = false; List <string> errorList = new List <string>(); List <string> parts = SplitCommandLine(args); for (int i = 0; i < parts.Count; ++i) { string arg = GetArgName(parts[i]); if (!IsArgumentName(parts[i])) { if (!handledDefault || ignoreAlreadyHandled) { parts[i] = string.Format("/{0}={1}", DefaultArgument, arg); arg = DefaultArgument; handledDefault = true; } } if (!Arguments.ContainsKey(arg)) { if (DefaultArgument != null && (!handledDefault || ignoreAlreadyHandled)) { parts[i] = string.Format("/{0}={1}", DefaultArgument, arg); arg = DefaultArgument; handledDefault = true; } else { errorList.Add(string.Format("Unknown option: '{0}'", arg)); errors = true; continue; } } Argument entry = Arguments[arg]; if (entry.NeedsValue) { // Not so simple cases: Collection and Option string value = ExtractValueFromArg(parts[i]); if (value == null && i < parts.Count - 1) { value = parts[i + 1]; if (Arguments.ContainsKey(GetArgName(value))) { value = null; } else { i++; } } if (value != null) { entry.SetValue(value); } else { // Missing argument errorList.Add(string.Format("Missing value for option '{0}'", arg)); errors = true; } } else // Simple case: a flag { entry.SetValue(true); } } foreach (KeyValuePair <string, Argument> kvp in Arguments) { Argument entry = kvp.Value; object value = entry.GetValue(); if (entry.IsRequired && value == null) { errorList.Add(string.Format("Missing value for option '{0}'", kvp.Key)); errors = true; } if (!entry.Validate(value)) { errorList.Add(string.Format("{0}: Invalid value {1}", kvp.Key, value)); errors = true; } } if (outErrors != null) { outErrors.Result = errorList.Distinct().ToArray(); } return(!errors); }
/// <summary>Processes a set of command line arguments.</summary> /// <param name="args"> /// Command line arguments to process. This is usally coming from your Main method. /// </param> /// <param name="outErrors"> /// Optional "out" parameter that holds error strings for every encountered error. /// </param> /// <returns> /// <c>true</c> if the arguments in <paramref name="args"/> are valid; otherwise /// <c>false</c> . /// </returns> public bool Validate(string[] args, OptionalOut <string[]> outErrors = null) { return(Validate(string.Join(" ", args), outErrors)); }
/// <summary>Processes a set of command line arguments.</summary> /// <param name="args"> /// Command line arguments to process. This is usally coming from your Main method. /// </param> /// <param name="outErrors"> /// Optional "out" parameter that holds error strings for every encountered error. /// </param> /// <returns> /// <c>true</c> if the arguments in <paramref name="args"/> are valid; otherwise /// <c>false</c> . /// </returns> public bool Validate(string args, OptionalOut <string[]> outErrors = null) { Reset(); bool ignoreAlreadyHandled = false; if (DefaultArgument != null) { ignoreAlreadyHandled = Arguments[DefaultArgument].SupportsMultipleValues; } bool handledDefault = false; bool errors = false; var errorList = new List <string>(); List <string> parts = CommandLineHelper.SplitCommandLine(args); for (int i = 0; i < parts.Count; ++i) { string arg = CommandLineHelper.GetArgName(parts[i]); if (!IsArgumentName(parts[i])) { Argument posArgument = GetArgumentForPosition(i); if (posArgument != null) { string argName = GetNameForArgument(posArgument); parts[i] = string.Format(CultureInfo.InvariantCulture, "/{0}={1}", argName, arg); arg = argName; } else if (DefaultArgument != null) { if (!handledDefault || ignoreAlreadyHandled) { parts[i] = string.Format(CultureInfo.InvariantCulture, "/{0}={1}", DefaultArgument, arg); arg = DefaultArgument; handledDefault = true; } } } if (!Arguments.ContainsKey(arg)) { if (DefaultArgument != null && (!handledDefault || ignoreAlreadyHandled)) { parts[i] = string.Format(CultureInfo.InvariantCulture, "/{0}={1}", DefaultArgument, arg); arg = DefaultArgument; handledDefault = true; } else { errorList.Add(string.Format(CultureInfo.CurrentCulture, "Unknown option: '{0}'", arg)); errors = true; continue; } } var entry = Arguments[arg]; if (entry.NeedsValue) { // Not so simple cases: Collection and Option var value = ExtractValueFromArg(parts[i]); if (value == null && i < parts.Count - 1) { value = parts[i + 1]; if (Arguments.ContainsKey(CommandLineHelper.GetArgName(value))) { value = null; } else { i++; } } if (value != null) { entry.Value = value; } else { // Missing argument errorList.Add(string.Format(CultureInfo.CurrentCulture, "Missing value for option '{0}'", arg)); errors = true; } } else // Simple case: a flag { entry.Value = true; } } foreach (var kvp in Arguments) { var entry = kvp.Value; var value = entry.Value; if (entry.IsRequired && value == null) { errorList.Add(string.Format(CultureInfo.CurrentCulture, "Missing value for option '{0}'", kvp.Key)); errors = true; } if (!entry.Validate(value)) { errorList.Add(string.Format(CultureInfo.CurrentCulture, "{0}: Invalid value '{1}'", kvp.Key, value)); errors = true; } } if (outErrors != null) { outErrors.Result = errorList.Distinct().ToArray(); } return(!errors); }