-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
60 lines (52 loc) · 2.02 KB
/
Program.cs
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
using System;
using System.Threading.Tasks;
using asuka.Commandline;
using asuka.Commandline.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using asuka.Installers;
using CommandLine;
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var services = new ServiceCollection();
services.InstallServicesInAssembly(configuration);
var serviceProvider = services.BuildServiceProvider();
async Task RunCommand(object options, string serviceKey)
{
var service = serviceProvider.GetKeyedService<ICommandLineParser>($"Cmd_{serviceKey}");
if (service == null)
{
Console.WriteLine($"Unknown command");
return;
}
await service.RunAsync(options);
}
var parser = Parser.Default
.ParseArguments<GetOptions, RecommendOptions, SearchOptions, RandomOptions, FileCommandOptions, ConfigureOptions, SeriesCreatorCommandOptions, CookieConfigureOptions>(args);
try
{
await parser.MapResult(
async (GetOptions opts) => { await RunCommand(opts, "Get"); },
async (RecommendOptions opts) => { await RunCommand(opts, "Recommend"); },
async (SearchOptions opts) => { await RunCommand(opts, "Search"); },
async (RandomOptions opts) => { await RunCommand(opts, "Random"); },
async (FileCommandOptions opts) => { await RunCommand(opts, "File"); },
async (ConfigureOptions opts) => { await RunCommand(opts, "Configure"); },
async (SeriesCreatorCommandOptions opts) => { await RunCommand(opts, "Series"); },
async (CookieConfigureOptions opts) => { await RunCommand(opts, "Cookie"); },
errors =>
{
foreach (var error in errors)
{
Console.WriteLine($"An error occured. Type: {error.Tag}");
}
return Task.FromResult(1);
});
return 0;
}
catch (Exception e)
{
Console.WriteLine($"An error occured. Message: {e.Message}");
return 1;
}