Things in dotnet/C# that make you go "what??"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB

using Cocona;
using DotWhat.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace DotWhat
{
internal class Program
{
private static readonly IEnumerable<What?> Whats =
Assembly.GetExecutingAssembly()
.GetTypes()
.Where(type => type.BaseType == typeof(What))
.Select(what => (What?)Activator.CreateInstance(what));
private static async Task<int> Main(string[] args)
{
await CoconaLiteApp.RunAsync<Program>(args).ConfigureAwait(false);
return 0;
}
public void Start([Argument] string? name = default)
{
if (string.IsNullOrEmpty(name))
{
Console.WriteLine("No what passed. Current available whats are:");
PrintWhats();
return;
}
var command = Whats.FirstOrDefault(what => what!.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (command is null)
{
Console.WriteLine("Invalid what passed. Current available whats are:");
PrintWhats();
return;
}
command.Run();
static void PrintWhats()
=> Whats.ToList().ForEach(what => Console.WriteLine(what!.Name.ToLower()));
}
}
}