<?php
/**
* Binds CLI arguments for dry-run scenarios with error logging.
*
* @param array $argv The command-line arguments array.
* @param array $dryRunOptions Optional array of dry-run options.
* @param string $logFile Optional path to the error log file.
* @return array An associative array containing the arguments, dry-run options, and errors.
*/
function bindCliArguments(array $argv, array $dryRunOptions = [], string $logFile = 'cli_errors.log'): array
{
$arguments = [];
$errors = [];
// Parse arguments
for ($i = 1; $i < count($argv); $i++) {
$arg = $argv[$i];
if ($arg === '--dry-run') {
$dryRunOptions = $dryRunOptions ? $dryRunOptions : []; // Initialize if not already set
} elseif (strpos($arg, '=') === 0) {
list($key, $value) = explode('=', $arg, 2);
$arguments[$key] = $value;
} else {
$arguments[] = $arg;
}
}
// Validate dry-run options
if (isset($dryRunOptions['enabled']) && $dryRunOptions['enabled'] === false) {
$errors[] = "Dry-run mode is disabled.";
}
// Log errors if any
if (!empty($errors)) {
error_log(implode("\n", $errors), 3, $logFile); // Log to file
}
return [
'arguments' => $arguments,
'dryRunOptions' => $dryRunOptions,
'errors' => $errors,
];
}
Add your comment