<?php
/**
* Aggregates command-line options with configuration file values for maintenance tasks.
*
* @param array $argv Command-line arguments.
* @param string $config_file Path to the configuration file.
* @return array Aggregated options.
*/
function aggregateOptions(array $argv, string $config_file): array
{
$options = [];
// Parse command-line arguments
$option_args = array_slice($argv, 1); // Skip script name
foreach ($option_args as $arg) {
if (strpos($arg, '=')) {
list($option, $value) = explode('=', $arg, 2);
$option = strtolower($option); // Convert to lowercase for consistency
$options[$option] = $value;
}
}
// Load configuration file
if (file_exists($config_file)) {
$config = require $config_file;
} else {
// Handle missing config file (e.g., return empty array or throw an error)
error_log("Configuration file not found: $config_file");
return $options; //Return only command line args
}
// Merge command-line options with configuration values
foreach ($config as $key => $value) {
if (isset($options[$key])) {
// Command-line option takes precedence
$options[$key] = $options[$key];
} else {
$options[$key] = $value;
}
}
return $options;
}
// Example usage (for testing - remove or modify as needed)
if (count($argv) > 1) {
$argv = $argv; //Ensure $argv is passed correctly
$config_file = 'config.php'; // Replace with your config file path
$aggregated_options = aggregateOptions($argv, $config_file);
print_r($aggregated_options);
}
?>
Add your comment