using ConsoleTemplate.Contexts; using ConsoleTemplate.Repositories; using ConsoleTemplate.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System.CommandLine; using System.Globalization; namespace ConsoleTemplate { internal class Program { static int Main(string[] args) { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection); var serviceProvider = serviceCollection.BuildServiceProvider(); var timerSvc = serviceProvider.GetRequiredService(); // Option var dateOption = new Option ( name: "--date", description: "Tangal Proses", isDefault: true, parseArgument: result => { if (result.Tokens.Count == 0) { return DateTime.Now; } string strtgl = result.Tokens.Single().Value; if (DateTime.TryParseExact(strtgl, "yyMMdd", null, DateTimeStyles.None, out DateTime tgl)) { return tgl; } else { result.ErrorMessage = "Format tanggal salah"; return DateTime.Now; } } ); dateOption.AddAlias("-d"); var threadOption = new Option ( name: "--thread", description: "Jumlah Thread", getDefaultValue: () => 8 ); threadOption.AddAlias("-t"); // Command // == Timer var rootCommand = new RootCommand("Proses RO PB"); rootCommand.AddOption(dateOption); rootCommand.AddOption(threadOption); rootCommand.SetHandler ( (ctx) => { timerSvc.Start( ctx.ParseResult.GetValueForOption(dateOption), ctx.ParseResult.GetValueForOption(threadOption) ); } ); return rootCommand.Invoke(args); } private static void ConfigureServices(ServiceCollection service) { // Build Configuration IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) //.SetBasePath(Environment.CurrentDirectory) .AddJsonFile("$safeprojectname$.config.json") .Build(); service.AddSingleton(configuration); service.AddSingleton(); service.AddSingleton(); service.AddSingleton(); service.AddScoped(); } } }