BaseCmd.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using qdr.app.tools.qfu.Commands.Aspects;
  2. using Quadarax.Foundation.Core.QConsole;
  3. using Quadarax.Foundation.Core.QConsole.Argument;
  4. using Quadarax.Foundation.Core.QConsole.Command.Base;
  5. using System.IO.Abstractions;
  6. namespace qdr.app.tools.qfu.Commands
  7. {
  8. abstract class BaseCmd : AbstractCommand
  9. {
  10. #region *** Properties ***
  11. protected string Source { get; private set; } = string.Empty;
  12. protected IFileSystem FileSystem => GetContext<QfuCommandContext>().FileSystem;
  13. #endregion
  14. #region *** Constructors ***
  15. protected BaseCmd(Engine engine) : base(engine)
  16. {
  17. }
  18. #endregion
  19. #region *** Protected Operations ***
  20. protected string ArgumentAsFileName(string argumentName)
  21. {
  22. var fileName = GetArgumentValueOrDefault<string>(argumentName);
  23. if (string.IsNullOrEmpty(fileName))
  24. throw new ArgumentException($"Argument '{argumentName}' is not defined or empty.", argumentName);
  25. return fileName;
  26. }
  27. protected void CheckSourceFileExists()
  28. {
  29. if (!FileSystem.File.Exists(Source))
  30. throw new ArgumentException($"Source file '{Source}' does not exist.", SourceAspect.ARG_SRC_NAME);
  31. }
  32. #endregion
  33. #region *** Overrides ***
  34. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  35. {
  36. var list = base.OnSetupArguments().ToList();
  37. list.AddRange(SourceAspect.SetupArguments());
  38. return list;
  39. }
  40. protected override void OnValidateArguments()
  41. {
  42. base.OnValidateArguments();
  43. Source = ArgumentAsFileName(SourceAspect.ARG_SRC_NAME);
  44. }
  45. #endregion
  46. }
  47. }