CmdPrint.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Quadarax.Foundation.Core.Console;
  5. using Quadarax.Foundation.Core.QConsole.Argument;
  6. using Quadarax.Foundation.Core.QConsole.Attributes;
  7. using Quadarax.Foundation.Core.QConsole.Command.Base;
  8. using Quadarax.Foundation.Core.QConsole.Value;
  9. using Quadarax.Foundation.Core.Value;
  10. namespace Quadarax.Foundation.Core.QConsole.Command.Defaults
  11. {
  12. [CommandDefinition]
  13. public class CmdPrint : AbstractCommand
  14. {
  15. public CmdPrint(Engine engine) : base(engine)
  16. {
  17. }
  18. public override string Name => Constants.Commands.Print.Name;
  19. public override string Description => Constants.Commands.Print.Description;
  20. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  21. {
  22. var arguments = base.OnSetupArguments().ToList();
  23. arguments.AddRange(
  24. new AbstractArgument []
  25. {
  26. new OrdinalArgument(0,
  27. false,
  28. Constants.Commands.Print.ArgCommandValue.Code,
  29. Constants.Commands.Print.Description,
  30. Constants.Commands.Print.ArgCommandValue.Hint,
  31. TypeValuesEnum.String,
  32. string.Empty,
  33. true),
  34. new FlagArgument(Constants.Commands.Print.ArgCommandClipboard.Code,
  35. Constants.Commands.Print.ArgCommandClipboard.Description,
  36. Constants.Commands.Print.ArgCommandClipboard.Hint,
  37. false),
  38. });
  39. return arguments;
  40. }
  41. protected override Result OnExecute()
  42. {
  43. var result = new Result();
  44. using (var writerGreen = new ConsoleWriter(ConsoleColor.Green))
  45. {
  46. using (var writerYellow = new ConsoleWriter(ConsoleColor.Yellow))
  47. {
  48. var valueArgument = GetArgument(0);
  49. if (valueArgument == null) throw new Exception("Value argument with ordinal 0 is required.");
  50. var value = valueArgument.Value.AsString();
  51. if (!value.StartsWith(QConsole.Defaults.RoleCharacters.SelectionVariablePrefixCharacter))
  52. throw new Exception($"Variable is defined as {QConsole.Defaults.RoleCharacters.SelectionVariablePrefixCharacter}<selection_ordinal>.<property_name>");
  53. var parts = value.Split(
  54. new string[]
  55. {
  56. QConsole.Defaults.RoleCharacters.SelectionVariablePrefixCharacter,
  57. QConsole.Defaults.RoleCharacters.SelectionVariablePropertyCharacter
  58. }, StringSplitOptions.RemoveEmptyEntries);
  59. if (parts.Length!=2)
  60. throw new Exception($"Variable is defined as {QConsole.Defaults.RoleCharacters.SelectionVariablePrefixCharacter}<selection_ordinal>.<property_name>");
  61. var ordinal = int.Parse(parts[0]);
  62. var selection = Engine.GetSelection(ordinal);
  63. if (selection == null)
  64. throw new Exception($"Selection variable '{value}' is not defined.");
  65. writerGreen.Write(value);
  66. writerGreen.Write(" = ");
  67. writerYellow.WriteLine(selection.GetPropertyValueAsString(parts[1]));
  68. var argument = GetArgument(Constants.Commands.Print.ArgCommandClipboard.Code);
  69. if (argument == null) throw new Exception($"Argument with code '{Constants.Commands.Print.ArgCommandClipboard.Code}' is required.");
  70. if ((bool) (argument.Value.Get() ?? false))
  71. {
  72. writerGreen.WriteLine("Value was stored in clipboard.");
  73. }
  74. }
  75. }
  76. return result;
  77. }
  78. }
  79. }