From e4a7073ea750c6bf3a82b35b81ba3883f8438fb4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 22:57:02 -0500 Subject: add GetPropertiesRecursively extension --- src/slskd/Common/Extensions.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 3e4f6e68..33faecf7 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -44,6 +44,30 @@ namespace slskd /// The new object deserialzied from the string. public static T ToObject(this string str) => JsonSerializer.Deserialize(str); + /// + /// Recursively retrieves all properties. + /// + /// The type from which to retrieve properties. + /// The list of properties. + public static IEnumerable GetPropertiesRecursively(this Type type) + { + var props = new List(); + + foreach (var prop in type.GetProperties()) + { + if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsArray || Nullable.GetUnderlyingType(prop.PropertyType) != null || new[] { typeof(string), typeof(decimal) }.Contains(prop.PropertyType)) + { + props.Add(prop); + } + else + { + props.AddRange(prop.PropertyType.GetPropertiesRecursively()); + } + } + + return props; + } + /// /// Deeply compares this object with the specified object and returns a list of properties that are different. /// -- cgit v1.2.3 From c5804abb31bd74f26101ad28acdd5c12273d2a8f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 22:57:27 -0500 Subject: find arguments backed by arrays and pass them as multiValuedArguments --- src/slskd/Program.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 9447bb7f..3821078b 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -335,6 +335,18 @@ namespace slskd { configurationFile = Path.GetFullPath(configurationFile); + var multiValuedArguments = typeof(Options) + .GetPropertiesRecursively() + .Where(p => p.PropertyType.IsArray) + .SelectMany(p => + p.CustomAttributes + .Where(a => a.AttributeType == typeof(ArgumentAttribute)) + .Select(a => new[] { a.ConstructorArguments[0].Value, a.ConstructorArguments[1].Value }) + .SelectMany(v => v)) + .Select(v => v.ToString()) + .Where(v => v != "\u0000") + .ToArray(); + return builder .AddDefaultValues( targetType: typeof(Options)) @@ -349,6 +361,7 @@ namespace slskd provider: new PhysicalFileProvider(Path.GetDirectoryName(configurationFile), ExclusionFilters.None)) // required for locations outside of the app directory .AddCommandLine( targetType: typeof(Options), + multiValuedArguments, commandLine: Environment.CommandLine); } -- cgit v1.2.3 From a0842636429bcecf87a2302ca8ebb685c77f8514 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 22:58:41 -0500 Subject: handle multiple values --- .../CommandLineConfigurationSource.cs | 38 +++++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index b49b2232..d338eef6 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; @@ -116,14 +117,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 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) + { + var elements = (List)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; + } } } } -- cgit v1.2.3 From 91fb504c2662eb5b374c19bd84962b557f4b5600 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 23:23:21 -0500 Subject: don't populate defaults for arrays --- .../Common/Configuration/DefaultValueConfigurationSource.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs index 15cf4070..b115d54d 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -99,11 +99,14 @@ namespace slskd.Configuration } else { - var value = property.GetValue(defaults); - - if (value != null) + if (!property.PropertyType.IsArray) { - Data[key] = value.ToString(); + var value = property.GetValue(defaults); + + if (value != null) + { + Data[key] = value.ToString(); + } } } } -- cgit v1.2.3 From 2e04802345be5d887e53351dafc13830f38a9cd7 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 23:23:35 -0500 Subject: add short names --- src/slskd/Options.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 75391271..92b5dd49 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -159,11 +159,11 @@ namespace slskd [RequiresRestart] public string InstanceName { get; private set; } = "default"; - [Argument(default, "list")] + [Argument('y', "list")] [EnvironmentVariable("LIST")] public string[] List { get; private set; } = Array.Empty(); - [Argument(default, "list-of-ints")] + [Argument('b', "list-of-ints")] [EnvironmentVariable("LIST_OF_INTS")] public int[] ListOfInts { get; private set; } = Array.Empty(); -- cgit v1.2.3