CmdHelp.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Quadarax.Foundation.Core.Console;
  6. using Quadarax.Foundation.Core.QConsole.Argument;
  7. using Quadarax.Foundation.Core.QConsole.Attributes;
  8. using Quadarax.Foundation.Core.QConsole.Command.Base;
  9. using Quadarax.Foundation.Core.QConsole.Extensions;
  10. using Quadarax.Foundation.Core.QConsole.Value;
  11. using Quadarax.Foundation.Core.Value;
  12. namespace Quadarax.Foundation.Core.QConsole.Command.Defaults
  13. {
  14. [CommandDefinition]
  15. public class CmdHelp : AbstractCommand
  16. {
  17. #region *** Properties ***
  18. public override string Name => Constants.Commands.Help.Name;
  19. public override string Description => Constants.Commands.Help.Description;
  20. #endregion
  21. #region *** Constructors ***
  22. public CmdHelp(Engine engine) : base(engine)
  23. {
  24. }
  25. #endregion
  26. #region *** Protected overrides ***
  27. protected override Result OnExecute()
  28. {
  29. if (!GetArgument(Constants.Commands.Help.ArgCommandName.Code).Value.HasValue)
  30. {
  31. if (!Engine.Configuration.IsConsoleDebug)
  32. {
  33. using (var writer = new ConsoleWriter(ConsoleColor.White))
  34. {
  35. writer.WriteLine(
  36. "To enable debug mode - more verbosity - use argument \"-dbg\" at the end of line.");
  37. }
  38. }
  39. using (var writer = new ConsoleWriter(ConsoleColor.Yellow))
  40. {
  41. writer.WriteLine("Console commands list:");
  42. }
  43. using (var writer = new ConsoleWriter(ConsoleColor.Green))
  44. {
  45. foreach (var command in Engine.Commands)
  46. {
  47. writer.WriteLine($"{command.Name}\t{command.Description}");
  48. }
  49. writer.WriteLine();
  50. }
  51. using (var writer = new ConsoleWriter(ConsoleColor.Yellow))
  52. {
  53. writer.WriteLine($"To view more info for specific command type {Name} <{GetArgument(Constants.Commands.Help.ArgCommandName.Code).Hint}>");
  54. }
  55. }
  56. else
  57. {
  58. var argument = GetArgument(Constants.Commands.Help.ArgCommandName.Code);
  59. var command = Engine.GetCommand(argument.Value.AsString());
  60. if (command == null)
  61. throw new Exception($"Unkonwn command '{argument.Value.AsString()}'");
  62. using (var writer = new ConsoleWriter(ConsoleColor.Green))
  63. {
  64. using (var writerWhite = new ConsoleWriter(ConsoleColor.White))
  65. {
  66. writer.Write($"Command: \t");
  67. writerWhite.WriteLine(command.Name);
  68. writer.Write($"Description: \t");
  69. writerWhite.WriteLine(command.Description);
  70. writer.Write($"Parameters: \t");
  71. writerWhite.WriteLine((command.Arguments.Length == 0 ? "none" : string.Empty));
  72. foreach (var arg in command.Arguments.OrderBy(x => x.IsPositional() ? 0 : 1))
  73. {
  74. var optional = arg.IsMandatory ? "Mandatory." : "Optional.";
  75. var argumentName = arg.IsPositional()
  76. ? $"<{arg.Code}>"
  77. : $"{Engine.Configuration.CharacterNamedArgumentSeparator}{arg.Code}";
  78. writer.WriteLine($"{argumentName}\t{optional}{arg.Description}");
  79. }
  80. var usage = new StringBuilder();
  81. writer.WriteLine();
  82. writer.WriteLine("Usage:");
  83. writer.Write($"\t{Engine.Configuration.CharacterLineIntroduce}");
  84. if (string.IsNullOrEmpty(command.CustomUsage))
  85. {
  86. usage.Append(command.CustomUsage).AppendLine();
  87. }
  88. else
  89. {
  90. usage.Append(command.Name);
  91. foreach (var arg in command.Arguments.OrderBy(x => x.IsPositional() ? 0 : 1))
  92. {
  93. usage.Append(" ");
  94. usage.Append(GetArgumentEntryAsDescription(arg));
  95. }
  96. }
  97. writerWhite.WriteLine(usage.ToString());
  98. if (!string.IsNullOrEmpty(command.Notes))
  99. {
  100. writer.WriteLine();
  101. writer.Write($"Notes: \t");
  102. writer.Write($"\t{command.Notes}");
  103. }
  104. if (!string.IsNullOrEmpty(command.Examples))
  105. {
  106. writer.WriteLine();
  107. writer.Write($"Examples: \t");
  108. writer.Write($"\t{command.Examples}");
  109. }
  110. }
  111. }
  112. }
  113. return new Result();
  114. }
  115. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  116. {
  117. var result = base.OnSetupArguments().ToList();
  118. result.AddRange( new AbstractArgument[]
  119. {
  120. new OrdinalArgument(0, true,
  121. Constants.Commands.Help.ArgCommandName.Code,
  122. Constants.Commands.Help.ArgCommandName.Description,
  123. Constants.Commands.Help.ArgCommandName.Hint,
  124. TypeValuesEnum.String,
  125. string.Empty,
  126. false)
  127. });
  128. return result;
  129. }
  130. #endregion
  131. #region *** Private Operations ***
  132. private string GetArgumentEntryAsDescription(AbstractArgument arg)
  133. {
  134. var argumentName = $"{Engine.Configuration.CharacterNamedArgumentSeparator}{arg.Code}";
  135. string argumentBody;
  136. if (arg.IsCodeAccessAllowed() && arg.IsPositional())
  137. argumentBody = "[" + argumentName + Engine.Configuration.CharacterNamedArgumentValueSeparator + "]" + arg.Hint;
  138. else if (arg.IsPositional())
  139. argumentBody = arg.Hint;
  140. else
  141. argumentBody = argumentName + Engine.Configuration.CharacterNamedArgumentValueSeparator + arg.Hint;
  142. if (!arg.IsMandatory)
  143. {
  144. argumentBody = "[" + argumentBody + "]";
  145. }
  146. return argumentBody;
  147. }
  148. #endregion
  149. }
  150. }