// Unrecognized $this->fatal('Unrecognized command line switch: ' . $switch); break; } return false; } } $DAEMON = new ChatDaemon(); set_error_handler(array($DAEMON, 'error_handler')); /// Check the parameters ////////////////////////////////////////////////////// unset($argv[0]); $commandline = implode(' ', $argv); if (strpos($commandline, '-') === false) { if (!empty($commandline)) { // We cannot have received any meaningful parameters $DAEMON->fatal('Garbage in command line'); } } else { // Parse command line $switches = preg_split('/(-{1,2}[a-zA-Z]+) */', $commandline, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); // Taking advantage of the fact that $switches is indexed with incrementing numeric keys // We will be using that to pass additional information to those switches who need it $numswitches = count($switches); // Fancy way to give a "hyphen" boolean flag to each "switch" $switches = array_map(create_function('$x', 'return array("str" => $x, "hyphen" => (substr($x, 0, 1) == "-"));'), $switches); for ($i = 0; $i < $numswitches; ++$i) { $switch = $switches[$i]['str']; $params = $i == $numswitches - 1 ? NULL : ($switches[$i + 1]['hyphen'] ? NULL : trim($switches[$i + 1]['str'])); if (substr($switch, 0, 2) == '--') { // Double-hyphen switch $DAEMON->cli_switch(strtolower(substr($switch, 2)), $params);
// Unrecognized. $this->fatal('Unrecognized command line switch: ' . $switch); break; } return false; } } $daemon = new ChatDaemon(); set_error_handler(array($daemon, 'error_handler')); // Check the parameters. unset($argv[0]); $commandline = implode(' ', $argv); if (strpos($commandline, '-') === false) { if (!empty($commandline)) { // We cannot have received any meaningful parameters. $daemon->fatal('Garbage in command line'); } } else { // Parse command line. $switches = preg_split('/(-{1,2}[a-zA-Z]+) */', $commandline, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); // Taking advantage of the fact that $switches is indexed with incrementing numeric keys. // We will be using that to pass additional information to those switches who need it. $numswitches = count($switches); // Fancy way to give a "hyphen" boolean flag to each "switch". $switches = array_map(create_function('$x', 'return array("str" => $x, "hyphen" => (substr($x, 0, 1) == "-"));'), $switches); for ($i = 0; $i < $numswitches; ++$i) { $switch = $switches[$i]['str']; $params = $i == $numswitches - 1 ? null : ($switches[$i + 1]['hyphen'] ? null : trim($switches[$i + 1]['str'])); if (substr($switch, 0, 2) == '--') { // Double-hyphen switch. $daemon->cli_switch(strtolower(substr($switch, 2)), $params);