-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
69 lines (65 loc) · 2.69 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
61
62
63
64
65
66
67
68
69
using System;
using System.Threading.Tasks;
using CodingSeb.ExpressionEvaluator;
using CommandLine;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using sample.console.Models.Arguments;
using sample.console.Services;
using sample.console.Services.Tasks;
using Serilog;
namespace sample.console
{
internal class Program
{
/// <summary>
/// Main routine
/// </summary>
/// <param name="args"></param>
/// <returns>Exit code</returns>
public static async Task<int> Main(string[] args)
{
try
{
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(context.Configuration)
.CreateLogger();
// Set up our console output class
services.AddSingleton<IConsoleOutput, ConsoleOutput>();
// Based upon verb/options, create services, including the task
var parserResult = Parser.Default.ParseArguments<CalculateOptions, StatisticsOptions>(args);
parserResult
.WithParsed<CalculateOptions>(options =>
{
services.AddSingleton<ExpressionEvaluator>();
services.AddSingleton(options);
services.AddSingleton<ITaskFactory, CalculateTaskFactory>();
})
.WithParsed<StatisticsOptions>(options =>
{
services.AddSingleton(options);
services.AddSingleton<ITaskFactory, StatisticsTaskFactory>();
});
})
.UseSerilog()
.Build();
// If a task was set up to run (i.e. valid command line params) then run it
// and return the results
var task = host.Services.GetService<ITaskFactory>();
return task == null
? -1 // This can happen on --help or invalid arguments
: await task.Launch();
}
catch (Exception ex)
{
// Note that this should only occur if something went wrong with building Host
await Console.Error.WriteLineAsync(ex.Message);
return -1;
}
}
}
}