diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-01-23 00:39:50 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-01-23 00:39:50 -0600 |
| commit | ff3b1fe36885442a6e40cac23ec8b186ef416e77 (patch) | |
| tree | 5e15c163d6179fd1f6d5129b97c458fda588c1e1 /src/slskd/Common/CommandLineArgumentConfigurationSource.cs | |
| parent | fc9a1aacfb9c4e005fd482ba37394840865e9e61 (diff) | |
| download | slskd-ff3b1fe36885442a6e40cac23ec8b186ef416e77.tar.gz | |
broken wip
Diffstat (limited to 'src/slskd/Common/CommandLineArgumentConfigurationSource.cs')
| -rw-r--r-- | src/slskd/Common/CommandLineArgumentConfigurationSource.cs | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/slskd/Common/CommandLineArgumentConfigurationSource.cs b/src/slskd/Common/CommandLineArgumentConfigurationSource.cs new file mode 100644 index 00000000..1762228d --- /dev/null +++ b/src/slskd/Common/CommandLineArgumentConfigurationSource.cs @@ -0,0 +1,93 @@ +namespace slskd +{ + using Microsoft.Extensions.Configuration; + using System; + using System.Collections.Generic; + using System.Linq; + using Utility.CommandLine; + + public class Argument + { + public string Description { get; set; } + public char ShortName { get; set; } + public string LongName { get; set; } + public string Key { get; set; } + } + + public static class CommandLineArgumentExtensions + { + public static IConfigurationBuilder MapCommandLineArguments(this IConfigurationBuilder builder, IEnumerable<Argument> map, string commandLine) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + return builder.MapCommandLineArguments(s => + { + s.Map = map; + s.CommandLine = commandLine; + }); + } + + public static IConfigurationBuilder MapCommandLineArguments(this IConfigurationBuilder builder, Action<CommandLineArgumentConfigurationSource> configureSource) + => builder.Add(configureSource); + } + + public class CommandLineArgumentConfigurationSource : IConfigurationSource + { + public IEnumerable<Argument> Map { get; set; } + public string CommandLine { get; set; } + + public IConfigurationProvider Build(IConfigurationBuilder builder) + { + return new CommandLineArgumentConfigurationProvider(this); + } + } + + public class CommandLineArgumentConfigurationProvider : ConfigurationProvider + { + public CommandLineArgumentConfigurationSource Source { get; set; } + + /// <summary> + /// Initializes a new instance with the specified source. + /// </summary> + /// <param name="source">The source settings.</param> + public CommandLineArgumentConfigurationProvider(CommandLineArgumentConfigurationSource source) + { + Source = source; + } + + public override void Load() + { + var dictionary = Arguments.Parse(Source.CommandLine).ArgumentDictionary; + + foreach (var item in Source.Map) + { + Console.WriteLine($"Item: {item.Key}"); + + if (string.IsNullOrEmpty(item.Key)) + { + continue; + } + + var arguments = new[] { item.ShortName.ToString(), item.LongName }.Where(i => !string.IsNullOrEmpty(i)); + + foreach (var argument in arguments) + { + if (dictionary.ContainsKey(argument)) + { + var value = dictionary[argument].ToString(); + + if (string.IsNullOrEmpty(value)) + { + value = "true"; + } + + Data[item.Key] = value; + } + } + } + } + } +} |
