Exemple #1
0
        private IParsingResultsDictionary parseConfigFromCommandLine()
        {
            var arguments          = new Queue <string>(_source.Args);
            var plainValueCounter  = 0;
            var resultsBuilder     = new ParsingResultsBuilder(_source.DuplicateKeyBehavior);
            var lastArgumentString = _source.Args.First();

            while (arguments.Any())
            {
                var currentArgumentString = arguments.Dequeue();
                var parsedArgument        = ArgumentType.CreateFrom(currentArgumentString);
                switch (parsedArgument)
                {
                case ArgumentType.SingleDash dashArgument:
                    handleSingleDashArgument(resultsBuilder, arguments, dashArgument);
                    break;

                case ArgumentType.DoubleDash doubleDashArgument:
                    handleDoubledashArgument(resultsBuilder, arguments, doubleDashArgument);
                    break;

                case ArgumentType.Assignment assignment:
                    if (_source.AllowDirectAssignments)
                    {
                        resultsBuilder.Add(assignment.Key, assignment.Value);
                    }
                    else
                    {
                        resultsBuilder.Error(new NotSupportedException($"Invalid argument '{assignment.Argument}' as direct assignments are not allowed."));
                    }
                    break;

                case ArgumentType.Invalid invalid:
                    resultsBuilder.Error(new ParsingException("Invalid argument.", $"Argument somewhere near: '{lastArgumentString.Limit(16)}'"));
                    break;

                case ArgumentType.Plain plain:
                    if (plainValueCounter < _valueDefinitions.Count)
                    {
                        resultsBuilder.Add(_valueDefinitions[plainValueCounter], plain.Argument);
                    }
                    else
                    {
                        resultsBuilder.Error(new IndexOutOfRangeException($"No more than {_valueDefinitions.Count} value arguments are allowed."));
                    }

                    plainValueCounter++;
                    break;
                }

                lastArgumentString = currentArgumentString;
            }

            if (_valueDefinitions.Count > plainValueCounter)
            {
                resultsBuilder.Error(new ValueRequiredException(string.Empty, $"Not all required values have been provided. Expected: {_valueDefinitions.Count}, Provided: {plainValueCounter}"));
            }

            return(resultsBuilder.GetResults());
        }
Exemple #2
0
 private static Unit addValueOptionToResults(ParsingResultsBuilder result,
                                             Queue <string> arguments,
                                             ArgumentDefinition.ValueOptionDefinition valueOption,
                                             Func <ArgumentDefinition.ValueOptionDefinition, string> argumentNameSelector)
 => arguments.Any()
         ? result.Add(valueOption.ConfigurationKey, arguments.Dequeue())
         : result.ErrorArgumentRequiresValue($"{argumentNameSelector(valueOption)}", $"The argument '{argumentNameSelector(valueOption)}' requires an additional value.");
        private static IParsingResultsDictionary readIniFromStream(Stream stream, KeyNameBehavior duplicateKeyBehavior)
        {
            var resultBuilder = new ParsingResultsBuilder(duplicateKeyBehavior);

            using (StreamReader streamReader = new StreamReader(stream))
            {
                var currentKeyPrefix = string.Empty;
                int lineCounter      = 0;
                while (streamReader.Peek() != -1)
                {
                    string currentLine = streamReader.ReadLine();
                    lineCounter++;

                    if (iniLineIsNotEmptyOrComment(currentLine))
                    {
                        var currentLineIsSectionHeading = getHeadingFromIniLine(currentLine);
                        currentKeyPrefix = currentLineIsSectionHeading.GetValue(currentKeyPrefix);

                        if (currentLineIsSectionHeading.IsNothing())
                        {
                            int assignmentOperatorPos = currentLine.IndexOf('=');
                            if (assignmentOperatorPos == 0)
                            {
                                resultBuilder.ErrorInvalidParsing($"Line #{lineCounter}", $"Line cannot start with assignment operator.{Environment.NewLine}  {currentLine}");
                            }
                            else if (assignmentOperatorPos < 0)
                            {
                                resultBuilder.Add(currentKeyPrefix + currentLine.Trim(), string.Empty, () => $"Error in line #{lineCounter}:");
                            }
                            else
                            {
                                string keyFullPathName = $"{currentKeyPrefix}{currentLine.Substring(0, assignmentOperatorPos).Trim()}";
                                string currentValue    = assignmentOperatorPos < currentLine.Trim().Length - 1
                                    ? getValueFromPossiblyQuotedString(currentLine.Substring(assignmentOperatorPos + 1).Trim())
                                    : string.Empty;

                                resultBuilder.Add(keyFullPathName, currentValue, () => $"Error in line #{lineCounter}:");
                            }
                        }
                    }
                }

                return(resultBuilder.GetResults());
            }
        }
Exemple #4
0
 private static Unit addSwitchToResults(ParsingResultsBuilder result, ArgumentDefinition.SwitchOptionDefinition currentDefinition)
 => result.Add(currentDefinition.ConfigurationKey, currentDefinition.ConfigurationValue);