//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
#pragma warning disable SA1649 // File name should match first type name
namespace slskd.Configuration
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Utility.CommandLine;
///
/// 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 to which to add.
/// 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).
///
/// The updated .
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 to which to add.
/// Configures the source.
/// The updated .
public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, Action configureSource)
=> builder.Add(configureSource);
}
///
/// A command line argument .
///
public class CommandLineConfigurationProvider : ConfigurationProvider
{
///
/// Initializes a new instance of the class.
///
/// The source settings.
public CommandLineConfigurationProvider(CommandLineConfigurationSource source)
{
Map = source.Map;
CommandLine = source.CommandLine;
NormalizeKey = source.NormalizeKey;
}
private string CommandLine { get; set; }
private IEnumerable Map { get; set; }
private bool NormalizeKey { get; set; }
///
/// Parses command line arguments from the specified string and maps them to the specified keys.
///
public override void Load()
{
var dictionary = Arguments.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;
}
}
}
}
}
///
/// Represents command line arguments as an .
///
public class CommandLineConfigurationSource : IConfigurationSource
{
///
/// Gets or sets the command line string from which to parse arguments.
///
public string CommandLine { get; set; }
///
/// Gets or sets a list of command line argument mappings.
///
public IEnumerable Map { get; set; }
///
/// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase).
///
public bool NormalizeKey { get; set; }
///
/// Builds the for this source.
///
/// The .
/// A .
public IConfigurationProvider Build(IConfigurationBuilder builder) => new CommandLineConfigurationProvider(this);
}
}
#pragma warning restore SA1649 // File name should match first type name