public bool ParseArguments(string[] commandLineArgs, ILogger logger, out IEnumerable<ArgumentInstance> argumentInstances)
{
if (commandLineArgs == null)
{
throw new ArgumentNullException("commandLineArgs");
}
if (logger == null)
{
throw new ArgumentNullException("logger");
}
bool parsedOk = true;
// List of values that have been recognized
IList<ArgumentInstance> recognized = new List<ArgumentInstance>();
foreach (string arg in commandLineArgs)
{
string prefix;
ArgumentDescriptor descriptor;
if (TryGetMatchingDescriptor(arg, out descriptor, out prefix))
{
string newId = descriptor.Id;
if (!descriptor.AllowMultiple && IdExists(newId, recognized))
{
string existingValue;
ArgumentInstance.TryGetArgumentValue(newId, recognized, out existingValue);
logger.LogError(Resources.ERROR_CmdLine_DuplicateArg, arg, existingValue);
parsedOk = false;
}
else
{
// Store the argument
string argValue = arg.Substring(prefix.Length);
recognized.Add(new ArgumentInstance(descriptor, argValue));
}
}
else
{
if (!this.allowUnrecognized)
{
logger.LogError(Resources.ERROR_CmdLine_UnrecognizedArg, arg);
parsedOk = false;
}
Debug.WriteLineIf(this.allowUnrecognized, "Ignoring unrecognized argument: " + arg);
}
}
// We'll check for missing arguments this even if the parsing failed so we output as much detail
// as possible about the failures.
parsedOk &= CheckRequiredArgumentsSupplied(recognized, logger);
argumentInstances = parsedOk ? recognized : Enumerable.Empty<ArgumentInstance>();
return parsedOk;
}