| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using DirectSight.Common; |
| | | 4 | | using DirectSight.Logging; |
| | | 5 | | |
| | | 6 | | namespace DirectSight; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Builder for <see cref="ReportConfiguration"/>. |
| | | 10 | | /// Creates instances of <see cref="ReportConfiguration"/> based on command line parameters. |
| | | 11 | | /// </summary> |
| | | 12 | | public class ReportConfigurationBuilder |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// The argument separators. |
| | | 16 | | /// </summary> |
| | 1 | 17 | | private static readonly char ArgumentSeparator = ';'; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="ReportConfiguration"/> class. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="cliArguments">The command line arguments stored as key value pairs.</param> |
| | | 23 | | /// <returns>The report configuration.</returns> |
| | | 24 | | public static ReportConfiguration Create(Dictionary<string, string> cliArguments) |
| | 3 | 25 | | { |
| | 3 | 26 | | var namedArguments = new Dictionary<string, string>(cliArguments); |
| | | 27 | | |
| | 3 | 28 | | var reportFilePatterns = Array.Empty<string>(); |
| | 3 | 29 | | var targetDirectory = string.Empty; |
| | 3 | 30 | | string debugMode = null; |
| | | 31 | | |
| | | 32 | | |
| | 3 | 33 | | if (namedArguments.TryGetValue(CommandLineArgumentNames.Reports, out string value)) |
| | 3 | 34 | | { |
| | 3 | 35 | | reportFilePatterns = value.SplitThatEnsuresGlobsAreSafe(ArgumentSeparator); |
| | 3 | 36 | | } |
| | | 37 | | |
| | 3 | 38 | | if (namedArguments.TryGetValue(CommandLineArgumentNames.TargetDirectory, out value)) |
| | 3 | 39 | | { |
| | 3 | 40 | | targetDirectory = value; |
| | 3 | 41 | | } |
| | | 42 | | |
| | 3 | 43 | | if (namedArguments.TryGetValue(CommandLineArgumentNames.DebugMode, out value)) |
| | 3 | 44 | | { |
| | 3 | 45 | | debugMode = value; |
| | 3 | 46 | | } |
| | | 47 | | |
| | 3 | 48 | | return new ReportConfiguration( |
| | 3 | 49 | | reportFilePatterns, |
| | 3 | 50 | | targetDirectory, |
| | 3 | 51 | | debugMode); |
| | 3 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Initializes a new instance of the <see cref="ReportConfiguration"/> class. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="args">The command line arguments.</param> |
| | | 58 | | /// <returns>The report configuration.</returns> |
| | | 59 | | internal static ReportConfiguration Create(string[] args) |
| | 3 | 60 | | { |
| | 3 | 61 | | ArgumentNullException.ThrowIfNull(args); |
| | | 62 | | |
| | 3 | 63 | | var namedArguments = new Dictionary<string, string>(); |
| | | 64 | | |
| | 27 | 65 | | foreach (var arg in args) |
| | 9 | 66 | | { |
| | 9 | 67 | | var match = CommandLineArgumentNames.CommandLineParameterRegex.Match(arg); |
| | | 68 | | |
| | 9 | 69 | | if (!match.Success) continue; |
| | | 70 | | |
| | 9 | 71 | | var matchKey = match.Groups["key"].Value; |
| | | 72 | | |
| | 9 | 73 | | if (namedArguments.TryGetValue(matchKey, out string value)) |
| | 0 | 74 | | { |
| | 0 | 75 | | ConsoleLogger.Warn( |
| | 0 | 76 | | "Duplicate command line parameter '{0}'. Using value '{1}'", |
| | 0 | 77 | | matchKey, |
| | 0 | 78 | | value); |
| | | 79 | | |
| | 0 | 80 | | continue; |
| | | 81 | | } |
| | | 82 | | |
| | 9 | 83 | | if (CommandLineArgumentNames.IsValid(matchKey)) |
| | 9 | 84 | | { |
| | 9 | 85 | | namedArguments[matchKey] = match.Groups["value"].Value; |
| | 9 | 86 | | continue; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | ConsoleLogger.Warn("Unknown command line parameter '{0}'", matchKey); |
| | 0 | 90 | | } |
| | | 91 | | |
| | 3 | 92 | | return Create(namedArguments); |
| | 3 | 93 | | } |
| | | 94 | | } |