/// <summary> /// Gets the appropriate parameter mapping. /// </summary> /// <returns>The mapping, or a <c>null</c> reference if no mapping matches the argument.</returns> /// <param name="argument">The command line argument.</param> /// <param name="shortNames">The mappings, indexed by their short names.</param> /// <param name="longNames">The mappings, indexed by their long names.</param> protected ParameterMapping GetMapping(string argument, IDictionary <string, ParameterMapping> shortNames, IDictionary <string, ParameterMapping> longNames) { ParameterMapping output = null; Match longParameter = LongParameter.Match(argument), shortParameter = ShortParameter.Match(argument); if (longParameter.Success && longNames.ContainsKey(longParameter.Groups[1].Value)) { output = longNames[longParameter.Groups[1].Value]; } else if (shortParameter.Success && shortNames.ContainsKey(shortParameter.Groups[1].Value)) { output = shortNames[shortParameter.Groups[1].Value]; } return(output); }
/// <summary> /// Parses the given command line arguments into a <see cref="ParsedParameters"/> instance. /// </summary> /// <param name="commandlineArguments">The command line arguments.</param> public ParsedParameters Parse(IList <string> commandlineArguments) { if (commandlineArguments == null) { throw new ArgumentNullException("commandlineArguments"); } IList <string> remainingArguments = new List <string>(); ICollection <object> flagParameters = new List <object>(); IDictionary <object, string> valueParameters = new Dictionary <object, string>(); IDictionary <string, ParameterMapping> shortNames, longNames; var allParameters = this.GetRegisteredParameters(); this.NormaliseNames(allParameters, out shortNames, out longNames); ParameterMapping currentValueParameter = null; foreach (string arg in commandlineArguments) { // If there is a current parameter expecting a value then we can just store this argument as that value. if (currentValueParameter != null && currentValueParameter.Behaviour == ParameterBehaviour.ValueRequired) { valueParameters.Add(currentValueParameter.Identifier, arg); currentValueParameter = null; continue; } ParameterMapping mapping = this.GetMapping(arg, shortNames, longNames); if (mapping == null && currentValueParameter != null) { valueParameters.Add(currentValueParameter.Identifier, arg); currentValueParameter = null; continue; } else if (mapping == null) { remainingArguments.Add(arg); continue; } if (currentValueParameter != null && currentValueParameter.Behaviour == ParameterBehaviour.ValueOptional) { valueParameters.Add(currentValueParameter.Identifier, String.Empty); currentValueParameter = null; } if (mapping.Behaviour == ParameterBehaviour.Switch) { flagParameters.Add(mapping.Identifier); } else { currentValueParameter = mapping; } } if (currentValueParameter != null && currentValueParameter.Behaviour == ParameterBehaviour.ValueOptional) { valueParameters.Add(currentValueParameter.Identifier, String.Empty); currentValueParameter = null; } return(new ParsedParameters(flagParameters, valueParameters, remainingArguments)); }