private static object ResolveArgumentValue(ArgumentRun argumentRun, IReadOnlyList <string> specifiedArguments, int startIndex, int endIndex) { if (startIndex == endIndex) { return(argumentRun.ResolveValue(specifiedArguments[startIndex])); } IList list = argumentRun.CreateCollection(endIndex - startIndex + 1); for (var i = startIndex; i <= endIndex; i++) { list.Add(argumentRun.ResolveValue(specifiedArguments[i])); } return(list); }
/// <summary> /// Process the specified arguments by verifying their usage, validating them and executing /// their handlers. /// </summary> /// <param name="specifiedArguments">The list of specified arguments.</param> /// <param name="argumentRuns">The argument run details.</param> /// <exception cref="ParserException"> /// Thrown if any of the validation or usage checks on the <see cref="ArgumentRun"/> objects /// fails. /// </exception> private static void ProcessArguments(IReadOnlyList <string> specifiedArguments, IReadOnlyList <ArgumentRun> argumentRuns) { // We already verified the number of specified arguments in the GetMatchingGroups method. // If there are no argument runs, there is nothing to process, so exit. if (argumentRuns.Count == 0) { return; } // Find the number of arguments that are required. int requiredArgumentCount = 0; while (requiredArgumentCount < argumentRuns.Count && !argumentRuns[requiredArgumentCount].Argument.IsOptional) { requiredArgumentCount++; } // Throw exception if not enough required arguments are specified. if (specifiedArguments.Count < requiredArgumentCount) { //TODO: The error message is too generic. Change to specify the first argument that's missing. throw new ParserException(ParserException.Codes.InvalidNumberOfArguments, Messages.InvalidNumberOfArguments); } // Iterate through all specified arguments and validate. // If validated, assign the value. for (int i = 0; i < argumentRuns.Count; i++) { ArgumentRun argumentRun = argumentRuns[i]; Argument argument = argumentRuns[i].Argument; // If there is a specified argument for this run. if (i < specifiedArguments.Count) { string argumentValue = specifiedArguments[i]; foreach (Validator validator in argument.Validators) { validator.Validate(argumentValue); } argumentRun.Assigned = true; argumentRun.Value = i == argumentRuns.Count - 1 && argument.MaxOccurences > 1 ? ResolveArgumentValue(argumentRun, specifiedArguments, i, specifiedArguments.Count - 1) : ResolveArgumentValue(argumentRun, specifiedArguments, i, i); } // No specified argument, but there is a default value. else if (argument.DefaultSetter != null) { // Note: For default values, none of the validators are run. This enables special // default values to be assigned that are outside the rules of validation. argumentRun.Assigned = true; object value = argument.DefaultSetter(); if (i == argumentRuns.Count - 1 && argument.MaxOccurences > 1) { IList list = argumentRun.CreateCollection(1); list.Add(value); argumentRun.Value = list; } else { argumentRun.Value = value; } } } }