private static CommandLineArgsConfigurationProvider createLoadedProvider(string[] arguments,
                                                                                 KeyNameBehavior keyBehavior,
                                                                                 ParsingResultsErrorBehavior errorBehavor = ParsingResultsErrorBehavior.OnFirstError,
                                                                                 bool allowAssignments   = false,
                                                                                 bool withRequiredValues = false)
        {
            var provider = createProvider(arguments, keyBehavior, errorBehavor, allowAssignments, withRequiredValues);

            provider.Load();
            return(provider);
        }
        private static CommandLineArgsConfigurationProvider createProvider(string[] arguments,
                                                                           KeyNameBehavior keyBehavior,
                                                                           ParsingResultsErrorBehavior errorBehavor = ParsingResultsErrorBehavior.OnFirstError,
                                                                           bool allowAssignments   = false,
                                                                           bool withRequiredValues = false)
        {
            var src = new CommandLineArgsConfigurationSource(withRequiredValues ? CommandLineArgsTestDefinitions.DefinitionsWithValues : CommandLineArgsTestDefinitions.Definitions, arguments)
            {
                DuplicateKeyBehavior   = keyBehavior,
                ParsingErrorBehavior   = errorBehavor,
                AllowDirectAssignments = allowAssignments
            };

            return(new CommandLineArgsConfigurationProvider(src));
        }
        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());
            }
        }
コード例 #4
0
 public ParsingResultsBuilder(KeyNameBehavior duplicateKeyBehavior)
 {
     _duplicateKeyBehavior = duplicateKeyBehavior;
     _internalDict         = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
 }
コード例 #5
0
 /// <summary>
 /// Adds a physical, non optional INI-file as configuration source to <paramref name="builder"/>,
 /// which will not be monitored for changes.
 /// </summary>
 /// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
 /// <param name="path">Path relative to the base path stored in
 /// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
 /// <param name="duplicateKeyBehavior">Determines, what happens if key names are parsed multiple times within the same section.</param>
 /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
 public static IConfigurationBuilder AddIniFile(this IConfigurationBuilder builder, string path, KeyNameBehavior duplicateKeyBehavior)
 => AddIniFile(builder, provider: null, path, optional: false, reloadOnChange: false, duplicateKeyBehavior);
コード例 #6
0
 /// <summary>
 /// Adds an INI-file configuration source to <paramref name="builder"/>.
 /// </summary>
 /// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
 /// <param name="provider">The <see cref="IFileProvider"/> to use to access the file.</param>
 /// <param name="path">Path relative to the base path stored in
 /// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
 /// <param name="optional">Whether the file is optional and thus would not cause exceptions.</param>
 /// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param>
 /// <param name="duplicateKeyBehavior">Determines, what happens if key names are parsed multiple times within the same section.</param>
 /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
 public static IConfigurationBuilder AddIniFile(this IConfigurationBuilder builder, IFileProvider provider, string path, bool optional, bool reloadOnChange, KeyNameBehavior duplicateKeyBehavior)
 {
     return(AddIniFile(builder, new IniFileConfigurationSource(path)
     {
         FileProvider = provider,
         Optional = optional,
         ReloadOnChange = reloadOnChange,
         DuplicateKeyBehavior = duplicateKeyBehavior
     }));
 }
コード例 #7
0
 /// <summary>
 /// Adds a command line configuration source to <paramref name="builder"/>.
 /// </summary>
 /// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
 /// <param name="args">The arguments provided via command line.</param>
 /// <param name="argumentDefinitions">
 /// The argument definitions, including those for switch options, value options
 /// AND required values. Must not contain ambiguous mappings for argument key names.</param>
 /// <param name="allowDirectAssignments">
 /// Determines, if assignments of the format 'key=value' are valid in command line
 /// and directly used as key/value-pairs for configuration source.
 /// </param>
 /// <param name="duplicateKeyBehavior">
 /// This value determines what happens, if a key name is parsed multiple times.
 /// </param>
 /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
 public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, IEnumerable <string> args, IEnumerable <ArgumentDefinition> argumentDefinitions, bool allowDirectAssignments, KeyNameBehavior duplicateKeyBehavior)
 => builder.Add(new CommandLineArgsConfigurationSource(argumentDefinitions, args)
 {
     AllowDirectAssignments = allowDirectAssignments, DuplicateKeyBehavior = duplicateKeyBehavior
 });