1. <?php
  2. /**
  3. * Parses command-line options with manual overrides.
  4. *
  5. * @param array $options An array of default option values.
  6. * @param array $overrides An array of option overrides (key => value).
  7. * @return array An associative array containing the parsed option values.
  8. */
  9. function parseCommandLineOptions(array $options, array $overrides): array
  10. {
  11. $parsedOptions = $options; // Start with default options
  12. // Apply overrides, overriding default values
  13. foreach ($overrides as $key => $value) {
  14. $parsedOptions[$key] = $value;
  15. }
  16. return $parsedOptions;
  17. }
  18. /**
  19. * Example usage and demonstration.
  20. */
  21. if (PHP_SAPI === 'cli') {
  22. // Default options
  23. $defaultOptions = [
  24. 'name' => 'default_name',
  25. 'port' => 8080,
  26. 'debug' => false,
  27. 'verbose' => true,
  28. ];
  29. // Command-line arguments (simulated for demonstration)
  30. $commandLineArgs = [
  31. 'name' => 'user_name',
  32. 'port' => 9000,
  33. 'debug' => true,
  34. ];
  35. // Parse the options
  36. $parsedOptions = parseCommandLineOptions($defaultOptions, $commandLineArgs);
  37. // Output the parsed options
  38. echo "<pre>";
  39. print_r($parsedOptions);
  40. echo "</pre>";
  41. }
  42. ?>

Add your comment