diff --git a/src/Microsoft.Extensions.Configuration.CommandLine/CommandLineConfigurationProvider.cs b/src/Microsoft.Extensions.Configuration.CommandLine/CommandLineConfigurationProvider.cs index 36d35204..b5eb4811 100644 --- a/src/Microsoft.Extensions.Configuration.CommandLine/CommandLineConfigurationProvider.cs +++ b/src/Microsoft.Extensions.Configuration.CommandLine/CommandLineConfigurationProvider.cs @@ -46,87 +46,89 @@ public override void Load() var data = new Dictionary(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;