<?php
/**
* Aggregates CLI arguments and configuration values.
*
* This is a temporary solution using a config file.
*/
// Default configuration
$config = [
'default_value' => 'default',
'array_value' => [],
'number_value' => 0,
];
// Load configuration from file
if (file_exists('config.ini')) {
$config_ini = parse_ini_file('config.ini');
$config = array_merge($config, $config_ini);
}
// Parse CLI arguments
$cli_args = getopt('a:n:d:h', ['array', 'number', 'default', 'help']);
// Process CLI arguments
if (isset($cli_args['a'])) {
$config['default_value'] = $cli_args['a']; // Set default value from CLI
}
if (isset($cli_args['n'])) {
$config['number_value'] = (int)$cli_args['n']; // Set number value from CLI, cast to int
}
if (isset($cli_args['d'])) {
$config['default_value'] = $cli_args['d']; // Override default value from CLI
}
if (isset($cli_args['array'])) {
$config['array_value'] = explode(',', $cli_args['array']); // Split array value by comma
}
if (isset($cli_args['help']) || isset($cli_args['h'])) {
echo "Usage: php script.php [-a <value>] [-n <number>] [-d <value>] [-h]\n";
echo " -a <value>: Sets the default value.\n";
echo " -n <number>: Sets the number value.\n";
echo " -d <value>: Sets the default value.\n";
echo " -h: Displays this help message.\n";
exit(0);
}
// Output aggregated values
echo "\nAggregated Values:\n";
echo "Default Value: " . $config['default_value'] . "\n";
echo "Number Value: " . $config['number_value'] . "\n";
echo "Array Value: " . implode(", ", $config['array_value']) . "\n";
?>
Add your comment