From 8807c9967600fb01282eb6c1aecaf57d714eb061 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jul 2021 22:27:14 -0500 Subject: wip --- src/slskd/Common/Configuration/CommandLineConfigurationSource.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/slskd/Common/Configuration/CommandLineConfigurationSource.cs') diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index 6cf59808..115a5be3 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -89,7 +89,9 @@ namespace slskd.Configuration /// public override void Load() { - var dictionary = Arguments.Parse(CommandLine).ArgumentDictionary; + Console.WriteLine(CommandLine); + var dictionary = Arguments.Parse(CommandLine, TargetType).ArgumentDictionary; + Console.WriteLine(dictionary.ToJson()); void Map(Type type, string path) { -- cgit v1.2.3 From 22e4e494b0baeb1983057f0b89dd03f97dfcdfc0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 20:05:59 -0500 Subject: add support for MultiValuedArguments --- .../Common/Configuration/CommandLineConfigurationSource.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/slskd/Common/Configuration/CommandLineConfigurationSource.cs') diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index 115a5be3..b49b2232 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -33,9 +33,10 @@ namespace slskd.Configuration /// /// The to which to add. /// The type from which to map properties. + /// An array of argument names which can be specified with multiple values. /// The command line string from which to parse arguments. /// The updated . - 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 +52,7 @@ namespace slskd.Configuration { s.TargetType = targetType; s.CommandLine = commandLine ?? Environment.CommandLine; + s.MultiValuedArguments = multiValuedArguments; }); } @@ -78,11 +80,13 @@ 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; } /// /// Parses command line arguments from the specified string and maps them to the corresponding keys. @@ -90,7 +94,7 @@ namespace slskd.Configuration public override void Load() { Console.WriteLine(CommandLine); - var dictionary = Arguments.Parse(CommandLine, TargetType).ArgumentDictionary; + var dictionary = Arguments.Parse(CommandLine, options => options.CombinableArguments = MultiValuedArguments).ArgumentDictionary; Console.WriteLine(dictionary.ToJson()); void Map(Type type, string path) @@ -149,6 +153,11 @@ namespace slskd.Configuration /// public Type TargetType { get; set; } + /// + /// Gets or sets an array of argument names which can be specified with multiple values. + /// + public string[] MultiValuedArguments { get; set; } = Array.Empty(); + /// /// Builds the for this source. /// -- 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(-) (limited to 'src/slskd/Common/Configuration/CommandLineConfigurationSource.cs') 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 c5a23135f01697e5fb234988a314be57c9e245d5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jul 2021 07:57:59 -0500 Subject: check for List<> to prevent regression --- src/slskd/Common/Configuration/CommandLineConfigurationSource.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/slskd/Common/Configuration/CommandLineConfigurationSource.cs') diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index d338eef6..0436c922 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -94,9 +94,7 @@ namespace slskd.Configuration /// public override void Load() { - Console.WriteLine(CommandLine); var dictionary = Arguments.Parse(CommandLine, options => options.CombinableArguments = MultiValuedArguments).ArgumentDictionary; - Console.WriteLine(dictionary.ToJson()); void Map(Type type, string path) { @@ -125,7 +123,7 @@ namespace slskd.Configuration // 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) + if (value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(List<>)) { var elements = (List)dictionary[argument]; -- cgit v1.2.3