1. <?php
  2. /**
  3. * Checks command-line options against predefined constraints.
  4. * Supports a dry-run mode.
  5. *
  6. * @param array $argv Command-line arguments.
  7. * @param array $expectedOptions Array of expected options and their constraints.
  8. * @param bool $dryRun Whether to perform a dry run (no actual actions).
  9. * @return array An array of validation errors. Empty array if valid.
  10. */
  11. function validateCommandLineOptions(array $argv, array $expectedOptions, bool $dryRun = false): array
  12. {
  13. $errors = [];
  14. foreach ($expectedOptions as $optionName => $constraints) {
  15. // Check if the option is present
  16. if (!isset($argv[$optionName])) {
  17. if ($constraints['required']) {
  18. $errors[] = "Option '$optionName' is required.";
  19. }
  20. continue; // Skip to the next option
  21. }
  22. $optionValue = $argv[$optionName];
  23. // Validate the option value based on the constraints
  24. if (isset($constraints['type'])) {
  25. switch ($constraints['type']) {
  26. case 'integer':
  27. if (!is_numeric($optionValue) || !filter_var($optionValue, FILTER_VALIDATE_INT)) {
  28. $errors[] = "Option '$optionName' must be an integer.";
  29. }
  30. break;
  31. case 'float':
  32. if (!is_numeric($optionValue) || !filter_var($optionValue, FILTER_VALIDATE_FLOAT)) {
  33. $errors[] = "Option '$optionName' must be a float.";
  34. }
  35. break;
  36. case 'string':
  37. //Simple string validation. Could add length constraints, etc.
  38. if(!is_string($optionValue)){
  39. $errors[] = "Option '$optionName' must be a string.";
  40. }
  41. break;
  42. default:
  43. $errors[] = "Unsupported type: " . $constraints['type'] . " for option '$optionName'.";
  44. }
  45. }
  46. if (isset($constraints['allowedValues'])) {
  47. if (!in_array($optionValue, $constraints['allowedValues'])) {
  48. $errors[] = "Option '$optionName' must be one of: " . implode(', ', $constraints['allowedValues']);
  49. }
  50. }
  51. if (isset($constraints['pattern'])) {
  52. if (!preg_match($constraints['pattern'], $optionValue)) {
  53. $errors[] = "Option '$optionName' does not match pattern: " . $constraints['pattern'];
  54. }
  55. }
  56. }
  57. return $errors;
  58. }
  59. // Example usage (for testing):
  60. /*
  61. $argv = ['--name', 'John Doe', '--age', '30', '--mode', 'debug'];
  62. $expectedOptions = [
  63. '--name' => ['required' => true, 'type' => 'string'],
  64. '--age' => ['required' => true, 'type' => 'integer'],
  65. '--mode' => ['required' => false, 'allowedValues' => ['debug', 'info', 'warning', 'error'], 'default' => 'info'],
  66. ];
  67. $errors = validateCommandLineOptions($argv, $expectedOptions, false); // Run validation
  68. if (empty($errors)) {
  69. echo "Command-line options are valid.\n";
  70. } else {
  71. echo "Validation errors:\n";
  72. foreach ($errors as $error) {
  73. echo "- " . $error . "\n";
  74. }
  75. }
  76. $argv2 = ['--name', 'John Doe', '--age', 'abc', '--mode', 'debug'];
  77. $errors2 = validateCommandLineOptions($argv2, $expectedOptions, false); // Run validation
  78. if (empty($errors2)) {
  79. echo "Command-line options are valid.\n";
  80. } else {
  81. echo "Validation errors:\n";
  82. foreach ($errors2 as $error) {
  83. echo "- " . $error . "\n";
  84. }
  85. }
  86. */
  87. ?>

Add your comment