1. <?php
  2. /**
  3. * Aggregates CLI arguments and configuration values.
  4. *
  5. * This is a temporary solution using a config file.
  6. */
  7. // Default configuration
  8. $config = [
  9. 'default_value' => 'default',
  10. 'array_value' => [],
  11. 'number_value' => 0,
  12. ];
  13. // Load configuration from file
  14. if (file_exists('config.ini')) {
  15. $config_ini = parse_ini_file('config.ini');
  16. $config = array_merge($config, $config_ini);
  17. }
  18. // Parse CLI arguments
  19. $cli_args = getopt('a:n:d:h', ['array', 'number', 'default', 'help']);
  20. // Process CLI arguments
  21. if (isset($cli_args['a'])) {
  22. $config['default_value'] = $cli_args['a']; // Set default value from CLI
  23. }
  24. if (isset($cli_args['n'])) {
  25. $config['number_value'] = (int)$cli_args['n']; // Set number value from CLI, cast to int
  26. }
  27. if (isset($cli_args['d'])) {
  28. $config['default_value'] = $cli_args['d']; // Override default value from CLI
  29. }
  30. if (isset($cli_args['array'])) {
  31. $config['array_value'] = explode(',', $cli_args['array']); // Split array value by comma
  32. }
  33. if (isset($cli_args['help']) || isset($cli_args['h'])) {
  34. echo "Usage: php script.php [-a <value>] [-n <number>] [-d <value>] [-h]\n";
  35. echo " -a <value>: Sets the default value.\n";
  36. echo " -n <number>: Sets the number value.\n";
  37. echo " -d <value>: Sets the default value.\n";
  38. echo " -h: Displays this help message.\n";
  39. exit(0);
  40. }
  41. // Output aggregated values
  42. echo "\nAggregated Values:\n";
  43. echo "Default Value: " . $config['default_value'] . "\n";
  44. echo "Number Value: " . $config['number_value'] . "\n";
  45. echo "Array Value: " . implode(", ", $config['array_value']) . "\n";
  46. ?>

Add your comment