Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for use as ConfigurationProvider #524

Open
stevefan1999-personal opened this issue Nov 22, 2022 · 3 comments
Open

Support for use as ConfigurationProvider #524

stevefan1999-personal opened this issue Nov 22, 2022 · 3 comments

Comments

@stevefan1999-personal
Copy link

Is your feature request related to a problem? Please describe.
I use .NET Generic Host for my CLI application but I have multiple layers of configuration. More specifically, I want to integrate CommandLineUtils as one of the ConfigurationProvider layers.

Describe the solution you'd like
Add a way or best practice towards the use of Custom ConfigurationProvider

Describe alternatives you've considered
Having two sets of options class and copy the CLI options into Host options.

Additional context
Add any other context or screenshots about the feature request here.

@stevefan1999-personal
Copy link
Author

This looks quite close to what I wanted to do

Console.OutputEncoding = Encoding.UTF8;

var cla = new CommandLineApplication<AppOptions>();
cla.Conventions
    .UseDefaultConventions();
var app = cla.Parse(args).SelectedCommand;

var textInfo = new CultureInfo("en-US", false).TextInfo;
var builder = Host.CreateApplicationBuilder();
builder.Configuration.AddInMemoryCollection(app
    .GetOptions()
    .ToDictionary(x => textInfo.ToTitleCase(x.LongName), x => x.Values)
    .SelectMany(kvp => kvp.Value.Count switch
    {
        1 => Enumerable.Repeat(KeyValuePair.Create(kvp.Key, kvp.Value[0]), 1),
        > 1 => kvp.Value.Select((value, idx) => KeyValuePair.Create($"{kvp.Key}:{idx}", value)),
        _ => null
    } ?? Array.Empty<KeyValuePair<string, string?>>()));
builder.Services.AddOptions<AppOptions>().BindConfiguration("");
builder.Services.AddSingleton(provider =>
{
    var options = provider.GetRequiredService<IOptions<AppOptions>>().Value;
    return new MaimaiConvertOption
    {
        ExtractImage = options.ExtractImage,
        BitRate = options.BitRate
    };
});
builder.Services.AddSingleton(provider =>
{
    var option = provider.GetRequiredService<MaimaiConvertOption>();
    return new MaimaiConverter(new WindowsPath(@"C:\maimai_universe"), option);
});

builder.Services.AddHostedService<AppService>();
using var host = builder.Build();
host.Run();

record AppOptions
{
    [Option("--extract-image")]
    public bool ExtractImage { get; init; }

    [Option("-b|--bit-rate")]
    public int? BitRate { get; init; }

    [Option("-P|--parallelism")]
    public int Parallelism { get; init; } = 1;
    [Option("-v|--verbose", CommandOptionType.MultipleValue)]
    public bool[]? Verbose { get; init; }
}

record AppService(MaimaiConverter Converter, IOptions<AppOptions> Options, ILogger<AppService> Logger) : IHostedService
{
    public Task StartAsync(CancellationToken cancellationToken)
    {
        var taskQueue = new List<Action>();

        foreach (var x in Converter.AvailableSongs?.Take(100) ?? ImmutableList<MusicData>.Empty)
        {
            async void Run()
            {
                Logger.LogInformation($"Processing {x.CueName.Id} - {x.CueName.Str}");
                var path = new WindowsPath("./output").Join($"{x.CueName.Id} - {x.CueName.Str}.mp3".ToValidFileName());
                {
                    await using var memStream = new MemoryStream();
                    await Converter.ExtractMusicTrackWithJacketByMusicData(x, memStream);

                    await using var file = path.Open(FileMode.Create);
                    memStream.WriteTo(file);
                }

                GC.Collect();

                Logger.LogInformation($"Processed {x.CueName.Str}");
            }

            taskQueue.Add(Run);
        }

        Parallel.Invoke(new() { MaxDegreeOfParallelism = Options.Value.Parallelism }, taskQueue.ToArray());
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

Copy link

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

@github-actions github-actions bot added the stale label Nov 23, 2023
@stevefan1999-personal
Copy link
Author

/remove-stale

@github-actions github-actions bot removed the stale label Nov 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant