AbstractListScopedCommand.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using Quadarax.Foundation.Core.QConsole;
  4. using Quadarax.Foundation.Core.QConsole.Argument;
  5. using Quadarax.Foundation.Core.QConsole.Value;
  6. // ReSharper disable InconsistentNaming
  7. namespace BO.Console.Commands.Base
  8. {
  9. internal abstract class AbstractListScopedCommand<TScopeEnum> : AbstractListCommand
  10. {
  11. #region *** Constants ***
  12. private const string CS_ARG_NAME_SCOPE = "scope";
  13. private const string CS_ARG_HINT_SCOPE = "<scope>";
  14. private const string CS_ARG_DESC_SCOPE = "Scope to filter: {0}. Must be one value.";
  15. #endregion
  16. #region *** Private Fields ***
  17. protected TScopeEnum Scope { get; private set; }
  18. #endregion
  19. #region *** Constructors ***
  20. protected AbstractListScopedCommand(Engine engine) : base(engine)
  21. {
  22. }
  23. #endregion
  24. #region *** Protected Overrides ***
  25. protected override void OnValidateArguments()
  26. {
  27. base.OnValidateArguments();
  28. var scope = GetArgumentValueOrDefault<string>(CS_ARG_NAME_SCOPE);
  29. if (!Enum.TryParse(typeof(TScopeEnum), scope, true, out var res))
  30. throw new ArgumentException(
  31. $"Argument {Engine.Configuration.CharacterNamedArgumentSeparator}{CS_ARG_NAME_SCOPE} invalid value. Must be one of: {string.Join(", ", Enum.GetNames(typeof(TScopeEnum)))} !",
  32. nameof(CS_ARG_NAME_SCOPE));
  33. Scope = (TScopeEnum)res;
  34. }
  35. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  36. {
  37. var args = new List<AbstractArgument>(base.OnSetupArguments());
  38. args.Add(new NamedArgument(CS_ARG_NAME_SCOPE, string.Format(CS_ARG_DESC_SCOPE, string.Join(", ", Enum.GetNames(typeof(TScopeEnum)))), CS_ARG_HINT_SCOPE,TypeValuesEnum.String,string.Empty,true));
  39. return args;
  40. }
  41. #endregion
  42. }
  43. }