<?php
/**
* Validates command-line options with retry intervals.
*
* @param array $options Associative array of command-line options.
* @param array $required_options Array of required options.
* @param array $validation_rules Array of validation rules for each option.
* @param int $retry_interval Seconds to wait between retries.
* @return bool True if validation is successful, false otherwise.
*/
function validateCommandLineOptions(array $options, array $required_options, array $validation_rules, int $retry_interval = 1): bool
{
// Check for required options
foreach ($required_options as $option) {
if (!isset($options[$option])) {
echo "Error: Required option '$option' is missing.\n";
return false;
}
}
// Validate each option
foreach ($validation_rules as $option => $rule) {
if (!isset($options[$option])) continue; // Skip if option is not present (shouldn't happen after required check)
if (!validateOption($options[$option], $rule)) {
echo "Error: Invalid value for option '$option'.\n";
return false;
}
}
return true;
}
/**
* Validates a single option value based on the provided rule.
*
* @param mixed $value The value of the option.
* @param array $rule The validation rule.
* @return bool True if the value is valid, false otherwise.
*/
function validateOption($value, array $rule): bool
{
switch ($rule['type']) {
case 'string':
if (!is_string($value)) {
return false;
}
if (isset($rule['min_length']) && strlen($value) < $rule['min_length']) {
return false;
}
if (isset($rule['max_length']) && strlen($value) > $rule['max_length']) {
return false;
}
return true;
case 'int':
if (!is_int($value)) {
return false;
}
if (isset($rule['min']) && $value < $rule['min']) {
return false;
}
if (isset($rule['max']) && $value > $rule['max']) {
return false;
}
return true;
case 'float':
if (!is_float($value)) {
return false;
}
if (isset($rule['min']) && $value < $rule['min']) {
return false;
}
if (isset($rule['max']) && $value > $rule['max']) {
return false;
}
return true;
case 'array':
if (!is_array($value)) {
return false;
}
return true; //No specific validation for array in this example
default:
echo "Error: Unknown validation type '$rule['type']'.\n";
return false;
}
}
// Example Usage:
$options = getopt(
['--name', '--age', '--count', '--output'],
['n', 'a', 'c', 'o'] //short option names
);
$options_array = array_map(function($value) {
return $value['value'];
}, $options);
$required_options = ['--name', '--age'];
$validation_rules = [
'--name' => ['type' => 'string', 'min_length' => 3, 'max_length' => 20],
'--age' => ['type' => 'int', 'min' => 0, 'max' => 150],
'--count' => ['type' => 'int', 'min' => 1],
'--output' => ['type' => 'string']
];
if (validateCommandLineOptions($options_array, $required_options, $validation_rules)) {
echo "Validation successful!\n";
// Proceed with processing the options
print_r($options_array);
} else {
echo "Validation failed.\n";
}
/**
* Helper function to parse command line arguments (example, use getopts for robust parsing)
* @param array $options
Add your comment