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

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
  1. using Cocona;
  2. using DotWhat.Contracts;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8. namespace DotWhat
  9. {
  10. internal class Program
  11. {
  12. private static readonly IEnumerable<What?> Whats =
  13. Assembly.GetExecutingAssembly()
  14. .GetTypes()
  15. .Where(type => type.BaseType == typeof(What))
  16. .Select(what => (What?)Activator.CreateInstance(what));
  17. private static async Task<int> Main(string[] args)
  18. {
  19. await CoconaLiteApp.RunAsync<Program>(args).ConfigureAwait(false);
  20. return 0;
  21. }
  22. public void Start([Argument] string? name = default)
  23. {
  24. if (string.IsNullOrEmpty(name))
  25. {
  26. Console.WriteLine("No what passed. Current available whats are:");
  27. PrintWhats();
  28. return;
  29. }
  30. var command = Whats.FirstOrDefault(what => what!.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
  31. if (command is null)
  32. {
  33. Console.WriteLine("Invalid what passed. Current available whats are:");
  34. PrintWhats();
  35. return;
  36. }
  37. command.Run();
  38. static void PrintWhats()
  39. => Whats.ToList().ForEach(what => Console.WriteLine(what!.Name.ToLower()));
  40. }
  41. }
  42. }