1. <?php
  2. /**
  3. * Validates command-line options with retry intervals.
  4. *
  5. * @param array $options Associative array of command-line options.
  6. * @param array $required_options Array of required options.
  7. * @param array $validation_rules Array of validation rules for each option.
  8. * @param int $retry_interval Seconds to wait between retries.
  9. * @return bool True if validation is successful, false otherwise.
  10. */
  11. function validateCommandLineOptions(array $options, array $required_options, array $validation_rules, int $retry_interval = 1): bool
  12. {
  13. // Check for required options
  14. foreach ($required_options as $option) {
  15. if (!isset($options[$option])) {
  16. echo "Error: Required option '$option' is missing.\n";
  17. return false;
  18. }
  19. }
  20. // Validate each option
  21. foreach ($validation_rules as $option => $rule) {
  22. if (!isset($options[$option])) continue; // Skip if option is not present (shouldn't happen after required check)
  23. if (!validateOption($options[$option], $rule)) {
  24. echo "Error: Invalid value for option '$option'.\n";
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. /**
  31. * Validates a single option value based on the provided rule.
  32. *
  33. * @param mixed $value The value of the option.
  34. * @param array $rule The validation rule.
  35. * @return bool True if the value is valid, false otherwise.
  36. */
  37. function validateOption($value, array $rule): bool
  38. {
  39. switch ($rule['type']) {
  40. case 'string':
  41. if (!is_string($value)) {
  42. return false;
  43. }
  44. if (isset($rule['min_length']) && strlen($value) < $rule['min_length']) {
  45. return false;
  46. }
  47. if (isset($rule['max_length']) && strlen($value) > $rule['max_length']) {
  48. return false;
  49. }
  50. return true;
  51. case 'int':
  52. if (!is_int($value)) {
  53. return false;
  54. }
  55. if (isset($rule['min']) && $value < $rule['min']) {
  56. return false;
  57. }
  58. if (isset($rule['max']) && $value > $rule['max']) {
  59. return false;
  60. }
  61. return true;
  62. case 'float':
  63. if (!is_float($value)) {
  64. return false;
  65. }
  66. if (isset($rule['min']) && $value < $rule['min']) {
  67. return false;
  68. }
  69. if (isset($rule['max']) && $value > $rule['max']) {
  70. return false;
  71. }
  72. return true;
  73. case 'array':
  74. if (!is_array($value)) {
  75. return false;
  76. }
  77. return true; //No specific validation for array in this example
  78. default:
  79. echo "Error: Unknown validation type '$rule['type']'.\n";
  80. return false;
  81. }
  82. }
  83. // Example Usage:
  84. $options = getopt(
  85. ['--name', '--age', '--count', '--output'],
  86. ['n', 'a', 'c', 'o'] //short option names
  87. );
  88. $options_array = array_map(function($value) {
  89. return $value['value'];
  90. }, $options);
  91. $required_options = ['--name', '--age'];
  92. $validation_rules = [
  93. '--name' => ['type' => 'string', 'min_length' => 3, 'max_length' => 20],
  94. '--age' => ['type' => 'int', 'min' => 0, 'max' => 150],
  95. '--count' => ['type' => 'int', 'min' => 1],
  96. '--output' => ['type' => 'string']
  97. ];
  98. if (validateCommandLineOptions($options_array, $required_options, $validation_rules)) {
  99. echo "Validation successful!\n";
  100. // Proceed with processing the options
  101. print_r($options_array);
  102. } else {
  103. echo "Validation failed.\n";
  104. }
  105. /**
  106. * Helper function to parse command line arguments (example, use getopts for robust parsing)
  107. * @param array $options

Add your comment