CmdHelp.cs 5.5 KB

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