AbstractListScopedCommand.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Quadarax.Foundation.Core.QConsole;
  5. using Quadarax.Foundation.Core.QConsole.Argument;
  6. using Quadarax.Foundation.Core.QConsole.Value;
  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. private string _scopeList;
  18. protected TScopeEnum Scope { get; private set; }
  19. #endregion
  20. #region *** Constructors ***
  21. protected AbstractListScopedCommand(Engine engine) : base(engine)
  22. {
  23. _scopeList = string.Join(", ", Enum.GetNames(typeof(TScopeEnum)));
  24. }
  25. #endregion
  26. #region *** Protected Overrides ***
  27. protected override void OnValidateArguments()
  28. {
  29. base.OnValidateArguments();
  30. var scope = GetArgumentValueOrDefault<string>(CS_ARG_NAME_SCOPE);
  31. if (!Enum.TryParse(typeof(TScopeEnum), scope, true, out var res))
  32. throw new ArgumentException(
  33. $"Argument {Engine.Configuration.CharacterNamedArgumentSeparator}{CS_ARG_NAME_SCOPE} invalid value. Must be one of: {_scopeList} !",
  34. nameof(CS_ARG_NAME_SCOPE));
  35. Scope = (TScopeEnum)res;
  36. }
  37. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  38. {
  39. var args = new List<AbstractArgument>(base.OnSetupArguments());
  40. args.Add(new NamedArgument(CS_ARG_NAME_SCOPE, string.Format(CS_ARG_DESC_SCOPE, _scopeList), CS_ARG_HINT_SCOPE,TypeValuesEnum.String,string.Empty,true));
  41. return args;
  42. }
  43. #endregion
  44. }
  45. }