aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/CommandLineArgumentConfigurationSource.cs
blob: 1762228df383fefbd1797de20e489f113b53e73b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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;
                    }
                }
            }
        }
    }
}