static NameValueCollection CollectSwitchesAndValuesFromArguments(IList<string> namedArguments, out bool firstArgumentIsSwitch)
{
firstArgumentIsSwitch = true;
var switchesAndValues = new NameValueCollection();
string currentKey = null;
var argumentRequiresValue = false;
for (var i = 0; i < namedArguments.Count; i = i + 1)
{
var arg = namedArguments[i];
// If the current (previous) argument doesn't require a value parameter and this is a switch, create new name/value entry for it, with a null value.
if (!argumentRequiresValue && arg.IsSwitchArgument())
{
currentKey = arg;
argumentRequiresValue = arg.ArgumentRequiresValue(i);
switchesAndValues.Add(currentKey, null);
}
// If this is a value (not a switch)
else if (currentKey != null)
{
// And if the current switch does not have a value yet and the value is not itself a switch, set its value to this argument.
if (string.IsNullOrEmpty(switchesAndValues[currentKey]))
{
switchesAndValues[currentKey] = arg;
}
// Otherwise add the value under the same switch.
else
{
switchesAndValues.Add(currentKey, arg);
}
// Reset the boolean argument flag so the next argument won't be ignored.
argumentRequiresValue = false;
}
else if (i == 0)
{
firstArgumentIsSwitch = false;
}
}
return switchesAndValues;
}