-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
73 lines (68 loc) · 2.58 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
70
71
72
73
using System.Data.SQLite;
using System.Reflection;
using System.Text;
using CommandLine;
using CommandLine.Text;
using Dapper;
using ElitechLog;
using ElitechLog.Consts;
using ElitechLogCLI.Verbs;
using NGettext;
namespace ElitechLogCLI;
public static class Program
{
static Program()
{
Console.OutputEncoding = Encoding.UTF8;
DefaultTypeMap.MatchNamesWithUnderscores = true;
// TODO: support other locales
var langFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"locales\en.mo");
if (File.Exists(langFile))
{
typeof(Db).Assembly.GetType("ElitechLog.T", false, false)
?.GetField("_Catalog", BindingFlags.NonPublic | BindingFlags.Static)
?.SetValue(null, new Catalog(File.OpenRead(langFile)));
}
Public.SysConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Resource\SysConfig.xml");
}
public static int Main(string[] args)
{
var parser = new Parser(cfg =>
{
cfg.CaseInsensitiveEnumValues = true;
cfg.AutoVersion = false;
});
var res = parser.ParseArguments<Info.Options, Pull.Options, Chart.Options, Set.Options, Migrate.Options, Export.Options, Reset.Options>(args);
try
{
return res.MapResult<Info.Options, Pull.Options, Chart.Options, Set.Options, Migrate.Options, Export.Options, Reset.Options, int>(
Info.Run, Pull.Run, Chart.Run, Set.Run, Migrate.Run, Export.Run, Reset.Run, _ =>
{
Console.WriteLine(HelpText.AutoBuild(res, help =>
{
help.AdditionalNewLineAfterOption = false;
help.AutoVersion = false;
help.AutoHelp = false;
return HelpText.DefaultParsingErrorsHandler(res, help);
}, e => e, maxDisplayWidth: parser.Settings.MaximumDisplayWidth));
return -1;
});
}
catch (SQLiteException ex)
{
var parts = ex.Message.Split(new[] { Environment.NewLine }, 2, StringSplitOptions.None);
Console.WriteLine("ERROR: " + parts[0] == parts[1] ? parts[0] : string.Join(": ", parts));
return ex.ErrorCode;
}
catch (ApplicationException ex)
{
Console.WriteLine("ERROR: " + ex.Message);
return ex.HResult;
}
catch (ArgumentException ex)
{
Console.WriteLine("ERROR: " + ex.Message);
return ex.HResult;
}
}
}