1. <?php
  2. /**
  3. * Limits output of command-line options for routine automation.
  4. *
  5. * This script takes command-line arguments and filters the output
  6. * based on a set of predefined options.
  7. */
  8. /**
  9. * Processes command-line arguments.
  10. *
  11. * @param array $argv The command-line arguments.
  12. * @param array $allowed_options An array of allowed options.
  13. * @return array An array of filtered arguments.
  14. */
  15. function process_arguments(array $argv, array $allowed_options): array
  16. {
  17. $filtered_args = [];
  18. $allowed_option_sets = [];
  19. //Define allowed option combinations
  20. $allowed_option_sets = [
  21. 'all' => [], //allow everything
  22. 'basic' => ['a', 'b'], //allow 'a' and 'b'
  23. 'verbose' => ['v', 'a'], //allow 'v' and 'a'
  24. ];
  25. // Check if any arguments are provided
  26. if (count($argv) > 1) {
  27. //Iterate through command line arguments.
  28. for ($i = 1; $i < count($argv); $i++) {
  29. $arg = $argv[$i];
  30. //Check if the argument is in allowed set.
  31. if (in_array($arg, $allowed_options)) {
  32. $filtered_args[] = $arg;
  33. }
  34. }
  35. }
  36. //Filter based on allowed options combinations
  37. if(isset($allowed_option_sets[$allowed_option_sets['all']])){
  38. $filtered_args = $allowed_option_sets['all'];
  39. } elseif (isset($allowed_option_sets[$allowed_option_sets['basic']])) {
  40. $filtered_args = $allowed_option_sets['basic'];
  41. } elseif (isset($allowed_option_sets[$allowed_option_sets['verbose']])){
  42. $filtered_args = $allowed_option_sets['verbose'];
  43. }
  44. return $filtered_args;
  45. }
  46. // Main execution
  47. $arguments = $argv; //Get command line arguments
  48. //Define allowed options
  49. $allowed_options = ['a', 'b', 'v', 'c'];
  50. //Process arguments
  51. $filtered_arguments = process_arguments($arguments, $allowed_options);
  52. //Output filtered arguments
  53. echo "<pre>";
  54. print_r($filtered_arguments);
  55. echo "</pre>";
  56. ?>

Add your comment