2020-01-24 13:48:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2021-07-08 07:56:31 +00:00
|
|
|
|
using System.Linq;
|
2021-09-04 09:33:59 +00:00
|
|
|
|
using System.Reflection;
|
2020-01-24 13:48:16 +00:00
|
|
|
|
using CommandLine;
|
2021-09-04 12:01:03 +00:00
|
|
|
|
using Tapeti.Cmd.ConsoleHelper;
|
2021-09-04 09:33:59 +00:00
|
|
|
|
using Tapeti.Cmd.Verbs;
|
2020-01-24 13:48:16 +00:00
|
|
|
|
|
|
|
|
|
namespace Tapeti.Cmd
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
|
|
|
|
public static int Main(string[] args)
|
|
|
|
|
{
|
2021-09-04 09:33:59 +00:00
|
|
|
|
var exitCode = 1;
|
|
|
|
|
var verbTypes = Assembly.GetExecutingAssembly().GetTypes()
|
|
|
|
|
.Where(t => t.GetCustomAttribute<ExecutableVerbAttribute>() != null)
|
|
|
|
|
.ToArray();
|
2021-07-08 07:56:31 +00:00
|
|
|
|
|
2021-09-07 13:56:59 +00:00
|
|
|
|
using var consoleWrapper = new ConsoleWrapper();
|
|
|
|
|
|
|
|
|
|
// ReSharper disable AccessToDisposedClosure
|
2021-09-04 09:33:59 +00:00
|
|
|
|
CommandLine.Parser.Default.ParseArguments(args, verbTypes.ToArray())
|
|
|
|
|
.WithParsed(o =>
|
2021-07-08 07:56:31 +00:00
|
|
|
|
{
|
2021-09-04 09:33:59 +00:00
|
|
|
|
try
|
2020-03-29 09:32:51 +00:00
|
|
|
|
{
|
2021-09-04 09:33:59 +00:00
|
|
|
|
var executableVerbAttribute = o.GetType().GetCustomAttribute<ExecutableVerbAttribute>();
|
|
|
|
|
var executer = Activator.CreateInstance(executableVerbAttribute.VerbExecuter, o) as IVerbExecuter;
|
|
|
|
|
|
|
|
|
|
// Should have been validated by the ExecutableVerbAttribute
|
|
|
|
|
Debug.Assert(executer != null, nameof(executer) + " != null");
|
2021-09-04 12:01:03 +00:00
|
|
|
|
|
|
|
|
|
executer.Execute(consoleWrapper);
|
2021-09-04 09:33:59 +00:00
|
|
|
|
exitCode = 0;
|
2021-07-08 07:56:31 +00:00
|
|
|
|
}
|
2021-09-04 09:33:59 +00:00
|
|
|
|
catch (Exception e)
|
2021-07-08 07:56:31 +00:00
|
|
|
|
{
|
2021-09-07 13:56:59 +00:00
|
|
|
|
using var consoleWriter = consoleWrapper.GetPermanentWriter();
|
|
|
|
|
consoleWriter.WriteLine(e.Message);
|
|
|
|
|
DebugConfirmClose(consoleWrapper);
|
2021-07-08 07:56:31 +00:00
|
|
|
|
}
|
2021-09-04 09:33:59 +00:00
|
|
|
|
})
|
|
|
|
|
.WithNotParsed(_ =>
|
|
|
|
|
{
|
2021-09-07 13:56:59 +00:00
|
|
|
|
DebugConfirmClose(consoleWrapper);
|
2021-09-04 09:33:59 +00:00
|
|
|
|
});
|
2021-09-07 13:56:59 +00:00
|
|
|
|
// ReSharper restore AccessToDisposedClosure
|
|
|
|
|
|
2021-09-04 09:33:59 +00:00
|
|
|
|
return exitCode;
|
2021-07-08 11:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-09-07 13:56:59 +00:00
|
|
|
|
private static void DebugConfirmClose(IConsole console)
|
2021-07-08 11:44:16 +00:00
|
|
|
|
{
|
2021-09-04 09:33:59 +00:00
|
|
|
|
if (!Debugger.IsAttached)
|
|
|
|
|
return;
|
2021-07-08 11:44:16 +00:00
|
|
|
|
|
2021-09-07 13:56:59 +00:00
|
|
|
|
using var consoleWriter = console.GetPermanentWriter();
|
|
|
|
|
consoleWriter.Confirm("Press any key to continue...");
|
2021-07-08 11:44:16 +00:00
|
|
|
|
}
|
2020-01-24 13:48:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|