professional-sql-11-lib-CommandLine.php / php
<?php // extend Exception class for custom exception type class CommandLineOptionException extends Exception { } class CommandLine { // define different configuration file types const CONFIG_TYPE_PLAIN = 1; const CONFIG_TYPE_INI = 2; // accept array of command line arguments, optional array of // valid/whitelist options static public function parseOptions(allowed = array()) { count = count(// retrive arguments and populate options array for (i < i++) { // retrieve arguments in form of --abc=foo if (preg_match('/^--([-A-Z0-9]+)=(.+)args[matches)) { if (empty(matches[1], options[matches[2]; } else { throw new CommandLineOptionException( 'Unrecognized option ' . // retrieve --abc arguments else if (substr(args[tmp = substr(i], 2); if (empty(tmp, options[tmp); } } // retrieve -abc foo, -abc, -a foo and -a arguments else if (i][0] == '-' && strlen(i]) > 1) { // set all arguments to true except for last in sequence for (j < strlen(i]) - 1; allowed) || in_array(i][allowed)) { args[j]] = true; } else { throw new CommandLineOptionException( 'Unrecognized option ' . i][// set last argument in compressed sequence tmp = substr(i], -1, 1); if (empty(tmp, // assign next args value if is value if (count && i + 1][0] != '-') { tmp] = i + 1]; // assign option as boolean else { options[tmp); } } // invalid option format else { throw new CommandLineOptionException( 'Invalid option format at ' . i]); } } return // process a configuration file and return its options as an array static public function parseConfigFile(file, options = array(); // process plain configuration file if (fp = fopen(fp)) { fp)); // skip blank lines and comments if (line)) { line); pieces[0]); pieces[1]); opt] = fp); } // process ini configuration file else if (options = parse_ini_file(options; } // prompt for user input, accept optional maximum input length and // callback function for validation static public function prompt(length = 255, label . ': '; callback) ? call_user_func(value) : // prompt for user input, accept optional default value, maximum input // length and callback function for validation static public function promptDefault(label, length = 255, label .= ' [' . value = self::prompt(length); if (!value = callback) ? call_user_func(value) :
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.