Template App 0.0.1
Template App Description
Loading...
Searching...
No Matches
C:/Users/Micha/Documents/GitHub/dotnet_template/App.cs
Go to the documentation of this file.
1using CommandLine;
2using CommandLine.Text;
4
5namespace App
6{
7 public class App
8 {
12 public const string name = "Template App";
16 public const string executable = "App";
20 public const string version = "0.0.1";
21
25 public class Options
26 {
30 [Option('n', "name", Required = true, HelpText = "The name to greet.")]
31 public string nameToGreet { get; set; }
32
36 [Option('g', "greeting", Required = false, HelpText = "The greeting to use.")]
37 public string? greeting { get; set; }
38
42 [Option("disable-logging", Required = false, Default = false, HelpText = "Disables writing information to log file.")]
43 public bool loggingOff { get; set; }
44
48 [Usage(ApplicationAlias = executable)]
49 public static IEnumerable<Example> Examples
50 {
51 get
52 {
53 return new List<Example>() {
54 new Example("Greet Luna with 'Hello'", new Options { nameToGreet = "Luna" }),
55 new Example("Greet Luna with 'Hi'", new Options { nameToGreet = "Luna",
56 greeting = "Hi" })
57 };
58 }
59 }
60 }
61
66 public static void RunApp(Options options)
67 {
68 // logger
69 var logger = new Logger($"{name.Replace(" ", "")}_v{version}_log.txt", !options.loggingOff);
70
71 try
72 {
73 // time of execution
74 var time = DateTime.Now.ToString("yyyy-MM-d_HH-mm");
75
76 // get commandline options
77 var nameToGreet = options.nameToGreet;
78 var greeting = options.greeting != null ?
79 options.greeting :
80 "Hello";
81
82 // start application
83 logger.info($"Starting {name} v{version} ...", ConsoleColor.Blue);
84 logger.info($"Time of start: {time}", writeToConsole: false);
85
86 // YOUR CODE
87 Console.WriteLine($"{greeting} {nameToGreet}!");
88 logger.info($"{greeting} {nameToGreet}!", writeToConsole: false);
89 logger.success($"Successfully greeted {nameToGreet}!");
90
91 // exit
92 logger.info($"{name} exited!", ConsoleColor.Blue);
93 return;
94 }
95 catch (Exception e)
96 {
97 // fatal unhandled error
98 logger.error($"A fatal error occured while running {name}! The specific exception that was thrown is:");
99 Console.WriteLine(e.ToString());
100 logger.error(e.ToString(), false);
101
102 // error -> exit
103 logger.info($"{name} exited!", ConsoleColor.Blue);
104 return;
105 }
106 finally
107 {
108 logger.dispose();
109 }
110 }
111
118 static void DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errors)
119 {
120 if (errors.IsVersion())
121 {
122 Console.ForegroundColor = ConsoleColor.Yellow;
123 Console.WriteLine($"{name} verson: {version}");
124 Console.ResetColor();
125 return;
126 }
127
128 var helpText = HelpText.AutoBuild(result, h =>
129 {
130 h.AdditionalNewLineAfterOption = false;
131 return HelpText.DefaultParsingErrorsHandler(result, h);
132 }, e => e);
133 helpText.AddPreOptionsLine("\nOPTIONS:");
134 Console.ForegroundColor = ConsoleColor.Yellow;
135 Console.WriteLine(helpText);
136 Console.ResetColor();
137 return;
138 }
139
144 public static void Main(string[] args)
145 {
146 var parser = new Parser(with => with.HelpWriter = null);
147 var parserResult = parser.ParseArguments<Options>(args);
148 parserResult
149 .WithParsed(RunApp)
150 .WithNotParsed(errors => DisplayHelp(parserResult, errors));
151 return;
152 }
153 }
154}
Util.Logger Logger
Definition App.cs:3
Commandline options.
Definition App.cs:26
bool loggingOff
Disables writing information to log file.
Definition App.cs:43
string nameToGreet
The name to greet.
Definition App.cs:31
static void RunApp(Options options)
The main application method run when all necessary arguments are given.
Definition App.cs:66
static void Main(string[] args)
Main function that is executed when application is run.
Definition App.cs:144
Class for logging messages to a file.
Definition Logger.cs:9
Definition App.cs:6
Definition Logger.cs:4