12 public const string name =
"Template App";
16 public const string executable =
"App";
20 public const string version =
"0.0.1";
30 [Option(
'n',
"name", Required =
true, HelpText =
"The name to greet.")]
31 public string nameToGreet {
get;
set; }
36 [Option(
'g',
"greeting", Required =
false, HelpText =
"The greeting to use.")]
37 public string? greeting {
get;
set; }
42 [Option(
"disable-logging", Required =
false, Default =
false, HelpText =
"Disables writing information to log file.")]
43 public bool loggingOff {
get;
set; }
48 [Usage(ApplicationAlias = executable)]
49 public static IEnumerable<Example> Examples
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",
69 var logger =
new Logger($
"{name.Replace(" ", "")}_v{version}_log.txt", !options.
loggingOff);
74 var time = DateTime.Now.ToString(
"yyyy-MM-d_HH-mm");
78 var greeting = options.greeting !=
null ?
83 logger.info($
"Starting {name} v{version} ...", ConsoleColor.Blue);
84 logger.info($
"Time of start: {time}", writeToConsole:
false);
87 Console.WriteLine($
"{greeting} {nameToGreet}!");
88 logger.info($
"{greeting} {nameToGreet}!", writeToConsole:
false);
89 logger.success($
"Successfully greeted {nameToGreet}!");
92 logger.info($
"{name} exited!", ConsoleColor.Blue);
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);
103 logger.info($
"{name} exited!", ConsoleColor.Blue);
118 static void DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errors)
120 if (errors.IsVersion())
122 Console.ForegroundColor = ConsoleColor.Yellow;
123 Console.WriteLine($
"{name} verson: {version}");
124 Console.ResetColor();
128 var helpText = HelpText.AutoBuild(result, h =>
130 h.AdditionalNewLineAfterOption =
false;
131 return HelpText.DefaultParsingErrorsHandler(result, h);
133 helpText.AddPreOptionsLine(
"\nOPTIONS:");
134 Console.ForegroundColor = ConsoleColor.Yellow;
135 Console.WriteLine(helpText);
136 Console.ResetColor();
144 public static void Main(
string[] args)
146 var parser =
new Parser(with => with.HelpWriter =
null);
147 var parserResult = parser.ParseArguments<
Options>(args);
150 .WithNotParsed(errors => DisplayHelp(parserResult, errors));