1
0
mirror of synced 2024-11-05 11:09:17 +00:00
Tapeti/Tapeti.Cmd/Program.cs

58 lines
1.8 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using CommandLine;
using Tapeti.Cmd.Verbs;
namespace Tapeti.Cmd
{
public class Program
{
public static int Main(string[] args)
{
var exitCode = 1;
var verbTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.GetCustomAttribute<ExecutableVerbAttribute>() != null)
.ToArray();
CommandLine.Parser.Default.ParseArguments(args, verbTypes.ToArray())
.WithParsed(o =>
{
try
{
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");
executer.Execute();
exitCode = 0;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
DebugConfirmClose();
}
})
.WithNotParsed(_ =>
{
DebugConfirmClose();
});
return exitCode;
}
private static void DebugConfirmClose()
{
if (!Debugger.IsAttached)
return;
Console.WriteLine("Press any Enter key to continue...");
Console.ReadLine();
}
}
}