FilterAspect.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Quadarax.Foundation.Core.QConsole.Argument;
  2. using Quadarax.Foundation.Core.QConsole.Command.Base;
  3. namespace qdr.app.tools.qfu.Commands.Aspects
  4. {
  5. internal class FilterAspect
  6. {
  7. #region *** Constants ***
  8. private const string ARG_FILTER_SEP = "|";
  9. public const string ARG_FILTER_NAME = "f";
  10. private const string ARG_FILTER_HINT = "filter";
  11. private const string ARG_FILTER_DESCR = "Search pattern for files or directories to delete (separated by pipe " + ARG_FILTER_SEP + "). Example: *.txt|*.log|temp*";
  12. #endregion
  13. #region *** Public Operations ***
  14. public static IEnumerable<AbstractArgument> SetupArguments()
  15. {
  16. return
  17. [
  18. new NamedArgument(ARG_FILTER_NAME, ARG_FILTER_DESCR, ARG_FILTER_HINT, Quadarax.Foundation.Core.QConsole.Value.TypeValuesEnum.String, string.Empty, false)
  19. ];
  20. }
  21. public static string[] GetSearchPatterns(AbstractCommand context)
  22. {
  23. if (context == null)
  24. throw new ArgumentNullException(nameof(context), "Command context cannot be null.");
  25. var filterArgument = context.Arguments.FirstOrDefault(x => string.Equals(x.Code, ARG_FILTER_NAME));
  26. if (filterArgument?.Value.Get() == null)
  27. return ["*"]; // Default pattern - match all
  28. var filterValue = filterArgument.Value.Get()?.ToString();
  29. if (string.IsNullOrWhiteSpace(filterValue))
  30. return ["*"]; // Default pattern - match all
  31. return ParseSearchPatterns(filterValue);
  32. }
  33. public static string[] ParseSearchPatterns(string filterArgument)
  34. {
  35. if (string.IsNullOrWhiteSpace(filterArgument))
  36. return ["*"];
  37. return filterArgument.Split(new[] { ARG_FILTER_SEP }, StringSplitOptions.RemoveEmptyEntries)
  38. .Select(f => f.Trim())
  39. .Where(f => !string.IsNullOrEmpty(f))
  40. .ToArray();
  41. }
  42. #endregion
  43. }
  44. }