1. <?php
  2. /**
  3. * Logs command-line options for dry-run scenarios with manual overrides.
  4. *
  5. * @param array $argv Command-line arguments.
  6. */
  7. function logDryRunOptions(array $argv): void
  8. {
  9. $log = [];
  10. // Store all command-line arguments
  11. $log['all_args'] = $argv;
  12. // Iterate through arguments, extracting option values
  13. for ($i = 1; $i < count($argv); $i++) {
  14. $arg = $argv[$i];
  15. if (strpos($arg, '-') === 0) {
  16. // It's an option
  17. $optionName = substr($arg, 1); // Remove the hyphen
  18. $log[$optionName] = $argv[$i + 1] ?? null; // Get the value, handling missing values
  19. $i += 1; // Skip the option value
  20. }
  21. }
  22. // Log the extracted options
  23. echo "<pre>";
  24. print_r($log);
  25. echo "</pre>";
  26. }
  27. // Example usage: (Uncomment to test)
  28. /*
  29. if (php_sapi_name() == 'cli') {
  30. $logDryRunOptions($argv);
  31. }
  32. */
  33. ?>

Add your comment