namespace slskd { using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; /// /// Defines a command line argument mapping. /// public record CommandLineArgument(char ShortName, string LongName, Type Type, string Key, string Description = null); /// /// Extension methods for adding . /// public static class CommandLineExtensions { /// /// Adds a command line argument configuration soruce to . /// /// The . /// A list of command line argument mappings. /// The command line string from which to parse arguments. /// A value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). /// public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, IEnumerable map, string commandLine = null, bool normalizeKey = true) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } return builder.AddCommandLine(s => { s.Map = map; s.CommandLine = commandLine ?? Environment.CommandLine; s.NormalizeKey = normalizeKey; }); } /// /// Adds a command line argument configuration source to . /// /// The . /// Configures the source. /// The . public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, Action configureSource) => builder.Add(configureSource); } /// /// Represents command line arguments as an . /// public class CommandLineConfigurationSource : IConfigurationSource { /// /// Builds the for this source. /// /// The . /// A . public IConfigurationProvider Build(IConfigurationBuilder builder) => new CommandLineConfigurationProvider(this); /// /// Gets or sets a list of command line argument mappings. /// public IEnumerable Map { get; set; } /// /// Gets or sets the command line string from which to parse arguments. /// public string CommandLine { get; set; } /// /// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). /// public bool NormalizeKey { get; set; } } /// /// A command line argument . /// public class CommandLineConfigurationProvider : ConfigurationProvider { private IEnumerable Map { get; set; } private string CommandLine { get; set; } private bool NormalizeKey { get; set; } /// /// Initializes a new instance with the specified source. /// /// The source settings. public CommandLineConfigurationProvider(CommandLineConfigurationSource source) { Map = source.Map; CommandLine = source.CommandLine; NormalizeKey = source.NormalizeKey; } /// /// Parses command line arguments from the specified string and maps them to the specified keys. /// public override void Load() { var dictionary = CommandLineArguments.Parse(CommandLine).ArgumentDictionary; foreach (CommandLineArgument item in Map) { var (shortName, longName, type, key, _) = item; if (NormalizeKey) { key = key?.Replace("-", string.Empty).Replace("_", string.Empty).ToLowerInvariant(); } if (string.IsNullOrEmpty(key)) { continue; } var arguments = new[] { shortName.ToString(), longName }.Where(i => !string.IsNullOrEmpty(i)); foreach (var argument in arguments) { if (dictionary.ContainsKey(argument)) { var value = dictionary[argument].ToString(); if (type == typeof(bool) && string.IsNullOrEmpty(value)) { value = "true"; } Data[item.Key] = value; } } } } } }