Comments (3)
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;
}
}
from commandlineutils.
Related Issues (20)
- RunCommandLineApplicationAsync fails after update 3.1.0->4.0.1 HOT 2
- Issue with `DefaultHelpTextGenerator` in `GenerateCommands` HOT 2
- how to get all options and values? HOT 4
- Exception when defining a property as Argument in a command and also in one of its sub commands
- [Question] Defining Options in Base Class and using them in derived class HOT 3
- Documentation error in Validation sample program HOT 2
- Add ResponseFileHandling to CommandLineContext so it can be passed through the static CommandLineApp.Execute HOT 2
- [Question] Unrecognized command or argument handling HOT 2
- Not possible to implement subcommand by using the inheritance method and with ExecuteAsync method HOT 3
- [Question] Trim Unused Code option HOT 5
- Nullable boolean Options aren't supported HOT 3
- [Question] Defining Options in Base Class and using them in derived class HOT 2
- [Question] - TargetInvocationException HOT 2
- Nuget package System.ComponentModel.Annotations V5.0.0 is deprecated HOT 2
- VersionOptionFromAssemblyAttributes - no documentation for template HOT 3
- [Question] Changes to DirectoryExists from 3.1.0 to 4.1.0 HOT 1
- [Question] Syntax of templates HOT 2
- Can I specify hidden options HOT 2
- Trimmed compilation fails with annotations HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from commandlineutils.