Help.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace TodoList
  5. {
  6. [Command(
  7. Name: "help",
  8. ShortDescription: "This is the command you typed to see this.",
  9. ErrorText: "Are you fucking kidding me?",
  10. LongHelpText: "You had to use the command correctly to see this, so I won't explain it in detail."
  11. )]
  12. internal class Help : ICommand
  13. {
  14. [DefaultSwitch(0)] public string topic = null;
  15. public void Invoke(Dictionary<String, Object> PipedArguments)
  16. {
  17. if (String.IsNullOrEmpty(topic))
  18. {
  19. Presentation.FillBar();
  20. Console.BackgroundColor = ConsoleColor.Black;
  21. foreach (var type in System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
  22. {
  23. var commandAttribute = type.GetCustomAttributes(true).FirstOrDefault(a => a is CommandAttribute) as CommandAttribute;
  24. if (commandAttribute != null)
  25. {
  26. Presentation.FillLine();
  27. Console.Write(commandAttribute.Name);
  28. foreach (var field in type.GetFields())
  29. {
  30. Console.Write(" -" + field.Name);
  31. if (field.GetCustomAttributes(true).Any(a => a is DefaultSwitchAttribute))
  32. Console.Write(" [dflt]");
  33. if (field.GetCustomAttributes(true).Any(a => a is GreedyArgumentAttribute))
  34. Console.Write(" [greedy]");
  35. }
  36. Console.WriteLine(" : " + commandAttribute.ShortDescription);
  37. }
  38. }
  39. Presentation.FillBar();
  40. Console.ResetColor();
  41. }
  42. else
  43. foreach (var type in System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
  44. {
  45. var commandAttribute = type.GetCustomAttributes(true).FirstOrDefault(a => a is CommandAttribute) as CommandAttribute;
  46. if (commandAttribute != null && commandAttribute.Name == topic)
  47. {
  48. Presentation.FillBar();
  49. Console.BackgroundColor = ConsoleColor.Black;
  50. Presentation.FillLine();
  51. Console.WriteLine(commandAttribute.Name);
  52. Presentation.FillLine();
  53. Console.WriteLine(commandAttribute.ShortDescription);
  54. foreach (var field in type.GetFields())
  55. {
  56. Presentation.FillLine();
  57. Console.Write(" -" + field.Name + " ");
  58. if (field.GetCustomAttributes(true).Any(a => a is DefaultSwitchAttribute))
  59. Console.Write("[dflt] ");
  60. if (field.GetCustomAttributes(true).Any(a => a is GreedyArgumentAttribute))
  61. Console.Write("[greedy] ");
  62. Console.Write(field.FieldType.ToString() + " ");
  63. var doc = field.GetCustomAttributes(true).FirstOrDefault(a => a is SwitchDocumentationAttribute) as SwitchDocumentationAttribute;
  64. if (doc != null)
  65. Console.Write(doc.Documentation);
  66. Console.WriteLine();
  67. }
  68. Presentation.FillLine();
  69. Console.WriteLine("Synonyms: " + String.Join(" ", commandAttribute.Synonyms));
  70. Presentation.FillLine();
  71. Console.WriteLine(commandAttribute.LongHelpText);
  72. Presentation.FillBar();
  73. Console.ResetColor();
  74. }
  75. }
  76. }
  77. }
  78. }