forked from michabirklbauer/dotnet_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cs
154 lines (139 loc) · 5.52 KB
/
App.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using CommandLine;
using CommandLine.Text;
using Logger = Util.Logger;
namespace App
{
public class App
{
/// <summary>
/// Name of the application.
/// </summary>
public const string name = "Template App";
/// <summary>
/// Exact name of the executable file.
/// </summary>
public const string executable = "App";
/// <summary>
/// Version of the application.
/// </summary>
public const string version = "0.0.1";
/// <summary>
/// Commandline options.
/// </summary>
public class Options
{
/// <summary>
/// The name to greet.
/// </summary>
[Option('n', "name", Required = true, HelpText = "The name to greet.")]
public string nameToGreet { get; set; }
/// <summary>
/// The greeting to use.
/// </summary>
[Option('g', "greeting", Required = false, HelpText = "The greeting to use.")]
public string? greeting { get; set; }
/// <summary>
/// Disables writing information to log file.
/// </summary>
[Option("disable-logging", Required = false, Default = false, HelpText = "Disables writing information to log file.")]
public bool loggingOff { get; set; }
/// <summary>
/// Usage examples.
/// </summary>
[Usage(ApplicationAlias = executable)]
public static IEnumerable<Example> Examples
{
get
{
return new List<Example>() {
new Example("Greet Luna with 'Hello'", new Options { nameToGreet = "Luna" }),
new Example("Greet Luna with 'Hi'", new Options { nameToGreet = "Luna",
greeting = "Hi" })
};
}
}
}
/// <summary>
/// The main application method run when all necessary arguments are given.
/// </summary>
/// <param name="options">Options parsed from the commandline.</param>
public static void RunApp(Options options)
{
// logger
var logger = new Logger($"{name.Replace(" ", "")}_v{version}_log.txt", !options.loggingOff);
try
{
// time of execution
var time = DateTime.Now.ToString("yyyy-MM-d_HH-mm");
// get commandline options
var nameToGreet = options.nameToGreet;
var greeting = options.greeting != null ?
options.greeting :
"Hello";
// start application
logger.info($"Starting {name} v{version} ...", ConsoleColor.Blue);
logger.info($"Time of start: {time}", writeToConsole: false);
// YOUR CODE
Console.WriteLine($"{greeting} {nameToGreet}!");
logger.info($"{greeting} {nameToGreet}!", writeToConsole: false);
logger.success($"Successfully greeted {nameToGreet}!");
// exit
logger.info($"{name} exited!", ConsoleColor.Blue);
return;
}
catch (Exception e)
{
// fatal unhandled error
logger.error($"A fatal error occured while running {name}! The specific exception that was thrown is:");
Console.WriteLine(e.ToString());
logger.error(e.ToString(), false);
// error -> exit
logger.info($"{name} exited!", ConsoleColor.Blue);
return;
}
finally
{
logger.dispose();
}
}
/// <summary>
/// Help displayed in case the necessary arguments for running the application are not given.
/// </summary>
/// <typeparam name="T">Type of the parser result.</typeparam>
/// <param name="result">Result of the parser.</param>
/// <param name="errors">Errors yielded by the parser.</param>
static void DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errors)
{
if (errors.IsVersion())
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"{name} verson: {version}");
Console.ResetColor();
return;
}
var helpText = HelpText.AutoBuild(result, h =>
{
h.AdditionalNewLineAfterOption = false;
return HelpText.DefaultParsingErrorsHandler(result, h);
}, e => e);
helpText.AddPreOptionsLine("\nOPTIONS:");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(helpText);
Console.ResetColor();
return;
}
/// <summary>
/// Main function that is executed when application is run.
/// </summary>
/// <param name="args">Arguments passed via commandline.</param>
public static void Main(string[] args)
{
var parser = new Parser(with => with.HelpWriter = null);
var parserResult = parser.ParseArguments<Options>(args);
parserResult
.WithParsed(RunApp)
.WithNotParsed(errors => DisplayHelp(parserResult, errors));
return;
}
}
}