Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Dispose command line args enumerator
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK committed Jul 28, 2016
1 parent 3ea6708 commit dc56902
Showing 1 changed file with 65 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,87 +46,89 @@ public override void Load()
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string key, value;

var enumerator = Args.GetEnumerator();
while (enumerator.MoveNext())
using (var enumerator = Args.GetEnumerator())
{
var currentArg = enumerator.Current;
var keyStartIndex = 0;

if (currentArg.StartsWith("--"))
{
keyStartIndex = 2;
}
else if (currentArg.StartsWith("-"))
{
keyStartIndex = 1;
}
else if (currentArg.StartsWith("/"))
while (enumerator.MoveNext())
{
// "/SomeSwitch" is equivalent to "--SomeSwitch" when interpreting switch mappings
// So we do a conversion to simplify later processing
currentArg = string.Format("--{0}", currentArg.Substring(1));
keyStartIndex = 2;
}

var separator = currentArg.IndexOf('=');

if (separator < 0)
{
// If there is neither equal sign nor prefix in current arugment, it is an invalid format
if (keyStartIndex == 0)
{
throw new FormatException(Resources.FormatError_UnrecognizedArgumentFormat(currentArg));
}
var currentArg = enumerator.Current;
var keyStartIndex = 0;

// If the switch is a key in given switch mappings, interpret it
if (_switchMappings != null && _switchMappings.ContainsKey(currentArg))
if (currentArg.StartsWith("--"))
{
key = _switchMappings[currentArg];
keyStartIndex = 2;
}
// If the switch starts with a single "-" and it isn't in given mappings , it is an invalid usage
else if (keyStartIndex == 1)
else if (currentArg.StartsWith("-"))
{
throw new FormatException(Resources.FormatError_ShortSwitchNotDefined(currentArg));
keyStartIndex = 1;
}
// Otherwise, use the switch name directly as a key
else
{
key = currentArg.Substring(keyStartIndex);
}

var previousKey = enumerator.Current;
if (!enumerator.MoveNext())
else if (currentArg.StartsWith("/"))
{
throw new FormatException(Resources.FormatError_ValueIsMissing(previousKey));
// "/SomeSwitch" is equivalent to "--SomeSwitch" when interpreting switch mappings
// So we do a conversion to simplify later processing
currentArg = string.Format("--{0}", currentArg.Substring(1));
keyStartIndex = 2;
}

value = enumerator.Current;
}
else
{
var keySegment = currentArg.Substring(0, separator);
var separator = currentArg.IndexOf('=');

// If the switch is a key in given switch mappings, interpret it
if (_switchMappings != null && _switchMappings.ContainsKey(keySegment))
if (separator < 0)
{
key = _switchMappings[keySegment];
// If there is neither equal sign nor prefix in current arugment, it is an invalid format
if (keyStartIndex == 0)
{
throw new FormatException(Resources.FormatError_UnrecognizedArgumentFormat(currentArg));
}

// If the switch is a key in given switch mappings, interpret it
if (_switchMappings != null && _switchMappings.ContainsKey(currentArg))
{
key = _switchMappings[currentArg];
}
// If the switch starts with a single "-" and it isn't in given mappings , it is an invalid usage
else if (keyStartIndex == 1)
{
throw new FormatException(Resources.FormatError_ShortSwitchNotDefined(currentArg));
}
// Otherwise, use the switch name directly as a key
else
{
key = currentArg.Substring(keyStartIndex);
}

var previousKey = enumerator.Current;
if (!enumerator.MoveNext())
{
throw new FormatException(Resources.FormatError_ValueIsMissing(previousKey));
}

value = enumerator.Current;
}
// If the switch starts with a single "-" and it isn't in given mappings , it is an invalid usage
else if (keyStartIndex == 1)
{
throw new FormatException(Resources.FormatError_ShortSwitchNotDefined(currentArg));
}
// Otherwise, use the switch name directly as a key
else
{
key = currentArg.Substring(keyStartIndex, separator - keyStartIndex);
var keySegment = currentArg.Substring(0, separator);

// If the switch is a key in given switch mappings, interpret it
if (_switchMappings != null && _switchMappings.ContainsKey(keySegment))
{
key = _switchMappings[keySegment];
}
// If the switch starts with a single "-" and it isn't in given mappings , it is an invalid usage
else if (keyStartIndex == 1)
{
throw new FormatException(Resources.FormatError_ShortSwitchNotDefined(currentArg));
}
// Otherwise, use the switch name directly as a key
else
{
key = currentArg.Substring(keyStartIndex, separator - keyStartIndex);
}

value = currentArg.Substring(separator + 1);
}

value = currentArg.Substring(separator + 1);
// Override value when key is duplicated. So we always have the last argument win.
data[key] = value;
}

// Override value when key is duplicated. So we always have the last argument win.
data[key] = value;
}

Data = data;
Expand Down

0 comments on commit dc56902

Please sign in to comment.