diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-07-27 08:53:18 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-27 08:53:18 -0500 |
| commit | 69d29c1eaa03992cf786d1d9b1ec1935dd85ef42 (patch) | |
| tree | 5253fcd096de96c3bf8c1cd3a9af0f303f4cb41f /src/slskd/Common/Configuration/CommandLineConfigurationSource.cs | |
| parent | 58bc401dab5d7eb3bc6cb07a22e0ee9c97ddf4bd (diff) | |
| parent | c5a23135f01697e5fb234988a314be57c9e245d5 (diff) | |
| download | slskd-69d29c1eaa03992cf786d1d9b1ec1935dd85ef42.tar.gz | |
Merge pull request #130 from slskd/lists
Accept option lists via command line and environment variables
Diffstat (limited to 'src/slskd/Common/Configuration/CommandLineConfigurationSource.cs')
| -rw-r--r-- | src/slskd/Common/Configuration/CommandLineConfigurationSource.cs | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index 6cf59808..0436c922 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -18,6 +18,7 @@ namespace slskd.Configuration { using System; + using System.Collections.Generic; using System.Linq; using System.Reflection; using Microsoft.Extensions.Configuration; @@ -33,9 +34,10 @@ namespace slskd.Configuration /// </summary> /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param> /// <param name="targetType">The type from which to map properties.</param> + /// <param name="multiValuedArguments">An array of argument names which can be specified with multiple values.</param> /// <param name="commandLine">The command line string from which to parse arguments.</param> /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns> - public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, Type targetType, string commandLine = null) + public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, Type targetType, string[] multiValuedArguments = null, string commandLine = null) { if (builder == null) { @@ -51,6 +53,7 @@ namespace slskd.Configuration { s.TargetType = targetType; s.CommandLine = commandLine ?? Environment.CommandLine; + s.MultiValuedArguments = multiValuedArguments; }); } @@ -78,18 +81,20 @@ namespace slskd.Configuration TargetType = source.TargetType; Namespace = TargetType.Namespace.Split('.').First(); CommandLine = source.CommandLine; + MultiValuedArguments = source.MultiValuedArguments; } private string CommandLine { get; set; } private string Namespace { get; set; } private Type TargetType { get; set; } + private string[] MultiValuedArguments { get; set; } /// <summary> /// Parses command line arguments from the specified string and maps them to the corresponding keys. /// </summary> public override void Load() { - var dictionary = Arguments.Parse(CommandLine).ArgumentDictionary; + var dictionary = Arguments.Parse(CommandLine, options => options.CombinableArguments = MultiValuedArguments).ArgumentDictionary; void Map(Type type, string path) { @@ -110,14 +115,41 @@ namespace slskd.Configuration { if (dictionary.ContainsKey(argument)) { - var value = dictionary[argument].ToString(); - - if (property.PropertyType == typeof(bool) && string.IsNullOrEmpty(value)) + // if the backing type is an array, it supports multiple values. + if (property.PropertyType.IsArray) { - value = "true"; + var value = dictionary[argument]; + + // Parse() will stuff multiple values into a List<T> if the argument name is + // in the list of those supporting multiple values, and more than one value was supplied. + // detect this, and add the values to the target. + if (value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(List<>)) + { + var elements = (List<object>)dictionary[argument]; + + for (int i = 0; i < elements.Count; i++) + { + Data[ConfigurationPath.Combine(key, i.ToString())] = elements[i].ToString(); + } + } + else + { + // there may have only been one value supplied, in which case the value is just a + // string. stick it in index 0 of the target. + Data[ConfigurationPath.Combine(key, "0")] = value.ToString(); + } } + else + { + var value = dictionary[argument].ToString(); - Data[key] = value; + if (property.PropertyType == typeof(bool) && string.IsNullOrEmpty(value)) + { + value = "true"; + } + + Data[key] = value; + } } } } @@ -148,6 +180,11 @@ namespace slskd.Configuration public Type TargetType { get; set; } /// <summary> + /// Gets or sets an array of argument names which can be specified with multiple values. + /// </summary> + public string[] MultiValuedArguments { get; set; } = Array.Empty<string>(); + + /// <summary> /// Builds the <see cref="CommandLineConfigurationProvider"/> for this source. /// </summary> /// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param> |
