cvs: pear /Console_Getargs Getargs.php /Console_Getargs/examples example.php
| From: | Scott Mattocks | Date: | Tue, 05 Oct 2004 20:59:41 +0000 |
| Subject: | cvs: pear /Console_Getargs Getargs.php /Console_Getargs/examples example.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-24630@lists.php.net to get a copy of this message | ||
scottmattocks Tue Oct 5 16:59:41 2004 EDT
Modified files:
/pear/Console_Getargs Getargs.php
/pear/Console_Getargs/examples example.php
Log:
Define a default option that will catch arguments that are passed without an option name. Normal
option rules still apply. Implementation of feature request #2428.
http://cvs.php.net/diff.php/pear/Console_Getargs/Getargs.php?r1=1.3&r2=1.4&ty=u Index: pear/Console_Getargs/Getargs.php diff -u pear/Console_Getargs/Getargs.php:1.3 pear/Console_Getargs/Getargs.php:1.4 --- pear/Console_Getargs/Getargs.php:1.3 Tue Sep 28 20:36:01 2004 +++ pear/Console_Getargs/Getargs.php Tue Oct 5 16:59:41 2004 @@ -16,7 +16,7 @@ // | Author: Bertrand Mansion <bmansion@mamasam.com> | // +----------------------------------------------------------------------+ // -// $Id: Getargs.php,v 1.3 2004/09/29 00:36:01 scottmattocks Exp $ +// $Id: Getargs.php,v 1.4 2004/10/05 20:59:41 scottmattocks Exp $ require_once 'PEAR.php'; @@ -78,6 +78,9 @@ * - Understands by default the --help, -h options * - Can return a formatted help text * - Arguments may be specified using the '=' syntax also. + * - Short option names may be concatenated (-dvw 100 == -d -v -w 100) + * - Can define a default option that will take any arguments added without + * an option name * * @todo Implement the parsing of comma delimited arguments * @author Bertrand Mansion <bmansion@mamasam.com> @@ -172,6 +175,14 @@ * echo 'Debug: '.($args->isDefined('d') ? "YES\n" : "NO\n"); * * </code> + * + * If you don't want to require any option name for a set of arguments, + * or if you would like any "leftover" arguments assigned by default, + * you can create an option named 'parameters' that will grab any + * arguments that cannot be assigned to another option. The rules for + * 'parameters' are still the same. If you specify that two values must + * be passed then two values must be passed. See the example script for + * a complete example. * * @param array associative array with keys being the options long name * @access public @@ -464,7 +475,6 @@ { // Go through the options and parse the arguments for each. for ($i = 0, $count = count($this->args); $i < $count; $i++) { - $arg = $this->args[$i]; if ($arg === '--') { @@ -484,6 +494,10 @@ if ($err === -1) { break; } + } elseif (isset($this->_config['parameters'])) { + // No flags at all. Try the parameters option. + $tempI = &$i - 1; + $err = $this->parseArg('parameters', true, $tempI); } else { $err = PEAR::raiseError('Unknown argument '.$arg, CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, @@ -515,7 +529,11 @@ if (!$isLong && !isset($this->_shortLong[$arg]) && strlen($arg) > 1) { $newArgs = array(); for ($i = 0; $i < strlen($arg); $i++) { - $newArgs[] = '-' . $arg{$i}; + if (array_key_exists($arg{$i}, $this->_shortLong)) { + $newArgs[] = '-' . $arg{$i}; + } else { + $newArgs[] = $arg{$i}; + } } // Add the new args to the array. array_splice($this->args, $pos, 1, $newArgs); @@ -591,7 +609,7 @@ // A value was passed after the option. if ($value !== '') { - // Argument is like -v 5 + // Argument is like -v5 if ($min == 1 && $max > 0) { // At least one argument is required for option. $this->updateValue($optname, $value); @@ -600,8 +618,8 @@ if ($max === 0) { // Argument passed but not expected. return PEAR::raiseError('Argument '.$optname.' does not take any value', - CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, - null, 'Console_Getargs_Options::setValue()'); + CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, + null, 'Console_Getargs_Options::setValue()'); } // Not enough arguments passed for this option. return PEAR::raiseError('Argument '.$optname.' expects more than one value', @@ -626,9 +644,16 @@ // Argument is a switch if (isset($this->args[$pos+1]) && $this->isValue($this->args[$pos+1])) { // What we thought was the next option was really an argument for this option. - return PEAR::raiseError('Argument '.$optname.' does not take any value', - CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, - null, 'Console_Getargs_Options::setValue()'); + // First update the value + $this->updateValue($optname, true); + // Then try to assign values to parameters. + if (isset($this->_config['parameters'])) { + return $this->setValue('parameters', '', ++$pos); + } else { + return PEAR::raiseError('Argument '.$optname.' does not take any value', + CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, + null, 'Console_Getargs_Options::setValue()'); + } } // Set the switch to on. $this->updateValue($optname, true); @@ -661,24 +686,39 @@ // Argument takes one or more values $added = 0; + // If trying to assign values to parameters, must go back one position. + if ($optname == 'parameters') { + $pos = max($pos - 1, -1); + } for ($i = $pos + 1; $i <= count($this->args); $i++) { if (isset($this->args[$i]) && $this->isValue($this->args[$i])) { // Add the argument value until the next option is hit. $this->updateValue($optname, $this->args[$i]); $added++; $pos++; - continue; + // Only keep trying if we haven't filled up yet. + if ($added < $max) { + continue; + } } if ($min > $added) { // There aren't enough arguments for this option. return PEAR::raiseError('Argument '.$optname.' expects at least '.$min.(($min > 1) ? ' values' : ' value'), CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, null, 'Console_Getargs_Options::setValue()'); - } else if ($max !== -1 && $added > $max) { + } else if ($max !== -1 && $added >= $max) { // Too many arguments for this option. - return PEAR::raiseError('Argument '.$optname.' expects maximum '.$max.' values', - CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, - null, 'Console_Getargs_Options::setValue()'); + // Try to add the extra options to parameters. + if (isset($this->_config['parameters']) && $optname != 'parameters') { + return $this->setValue('parameters', '', ++$pos); + } elseif ($optname == 'parameters' && !isset($this->args[$i + 1])) { + $pos += $added; + break; + } else { + return PEAR::raiseError('Argument '.$optname.' expects maximum '.$max.' values', + CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, + null, 'Console_Getargs_Options::setValue()'); + } } break; } http://cvs.php.net/diff.php/pear/Console_Getargs/examples/example.php?r1=1.3&r2=1.4&ty=u Index: pear/Console_Getargs/examples/example.php diff -u pear/Console_Getargs/examples/example.php:1.3 pear/Console_Getargs/examples/example.php:1.4 --- pear/Console_Getargs/examples/example.php:1.3 Thu Sep 30 12:37:27 2004 +++ pear/Console_Getargs/examples/example.php Tue Oct 5 16:59:41 2004 @@ -2,10 +2,10 @@ /** * Example for Console_Getargs class * - * $Id: example.php,v 1.3 2004/09/30 16:37:27 scottmattocks Exp $ + * $Id: example.php,v 1.4 2004/10/05 20:59:41 scottmattocks Exp $ */ -require_once('Console/Getargs.php'); +require_once('../Getargs.php'); $config = array( // Option takes 2 values @@ -19,7 +19,9 @@ // Option takes from 1 to an unlimited number of values 'filters' => array('short' => 'fi', 'min' => 1, 'max' => -1, 'desc' => 'Set the filters to be applied to the image upon conversion. The filters will be used in the order they are set.'), // Option accept 1 value or nothing. If nothing, then the default value is used - 'verbose' => array('short' => 'v', 'min' => 0, 'max' => 1, 'desc' => 'Set the verbose level.', 'default' => 3) + 'verbose' => array('short' => 'v', 'min' => 0, 'max' => 1, 'desc' => 'Set the verbose level.', 'default' => 3), + // Parameters. Anything leftover at the end of the command line is added. + 'parameters' => array('min' => 0, 'max' => 2, 'desc' => 'Set the application parameters.', 'default' => 'foo1') ); $args =& Console_Getargs::factory($config); @@ -45,15 +47,35 @@ } echo 'Width: '.$args->getValue('w')."\n"; echo 'Debug: '.($args->isDefined('d') ? "YES\n" : "NO\n"); +echo 'Parameters: '.($args->isDefined('parameters') ? is_array($args->getValue('parameters')) ? implode(', ', $args->getValue('parameters')) : $args->getValue('parameters') : "undefined") . "\n"; // Test with: // ---------- +// Get the help message // php -q example.php -h +// +// Pass two files // php -q example.php -v -f src.tiff dest.tiff +// +// Set verbose level 5, pass two files, and debug // php -q example.php -v5 -f src.tiff dest.tiff -d +// +// Set verbose level 1, pass two files, debug, and set width to 100 // php -q example.php -v 1 -f src.tiff dest.tiff -d --width=100 +// +// Set verbose (defaults to 3), pass two files, and pass two filters // php -q example.php -v -f src.tiff dest.tiff -fi sharp blur +// +// Set three formats // php -q example.php --format gif jpeg png +// +// Debug, set verbose to default level and width to 100 // php -q example.php -dvw 100 +// +// Pass two application parameters +// php -q example.php foo1 foo2 +// +// Debug, set verbose level 5, pass two files, and pass two application parameters +// php -q examples/example.php -dv5 -f src.tiff dest.tiff foo1 foo2 ?>
http://cvs.php.net/diff.php/pear/Console_Getargs/Getargs.php?r1=1.3&r2=1.4&ty=u Index: pear/Console_Getargs/Getargs.php diff -u pear/Console_Getargs/Getargs.php:1.3 pear/Console_Getargs/Getargs.php:1.4 --- pear/Console_Getargs/Getargs.php:1.3 Tue Sep 28 20:36:01 2004 +++ pear/Console_Getargs/Getargs.php Tue Oct 5 16:59:41 2004 @@ -16,7 +16,7 @@ // | Author: Bertrand Mansion <bmansion@mamasam.com> | // +----------------------------------------------------------------------+ // -// $Id: Getargs.php,v 1.3 2004/09/29 00:36:01 scottmattocks Exp $ +// $Id: Getargs.php,v 1.4 2004/10/05 20:59:41 scottmattocks Exp $ require_once 'PEAR.php'; @@ -78,6 +78,9 @@ * - Understands by default the --help, -h options * - Can return a formatted help text * - Arguments may be specified using the '=' syntax also. + * - Short option names may be concatenated (-dvw 100 == -d -v -w 100) + * - Can define a default option that will take any arguments added without + * an option name * * @todo Implement the parsing of comma delimited arguments * @author Bertrand Mansion <bmansion@mamasam.com> @@ -172,6 +175,14 @@ * echo 'Debug: '.($args->isDefined('d') ? "YES\n" : "NO\n"); * * </code> + * + * If you don't want to require any option name for a set of arguments, + * or if you would like any "leftover" arguments assigned by default, + * you can create an option named 'parameters' that will grab any + * arguments that cannot be assigned to another option. The rules for + * 'parameters' are still the same. If you specify that two values must + * be passed then two values must be passed. See the example script for + * a complete example. * * @param array associative array with keys being the options long name * @access public @@ -464,7 +475,6 @@ { // Go through the options and parse the arguments for each. for ($i = 0, $count = count($this->args); $i < $count; $i++) { - $arg = $this->args[$i]; if ($arg === '--') { @@ -484,6 +494,10 @@ if ($err === -1) { break; } + } elseif (isset($this->_config['parameters'])) { + // No flags at all. Try the parameters option. + $tempI = &$i - 1; + $err = $this->parseArg('parameters', true, $tempI); } else { $err = PEAR::raiseError('Unknown argument '.$arg, CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, @@ -515,7 +529,11 @@ if (!$isLong && !isset($this->_shortLong[$arg]) && strlen($arg) > 1) { $newArgs = array(); for ($i = 0; $i < strlen($arg); $i++) { - $newArgs[] = '-' . $arg{$i}; + if (array_key_exists($arg{$i}, $this->_shortLong)) { + $newArgs[] = '-' . $arg{$i}; + } else { + $newArgs[] = $arg{$i}; + } } // Add the new args to the array. array_splice($this->args, $pos, 1, $newArgs); @@ -591,7 +609,7 @@ // A value was passed after the option. if ($value !== '') { - // Argument is like -v 5 + // Argument is like -v5 if ($min == 1 && $max > 0) { // At least one argument is required for option. $this->updateValue($optname, $value); @@ -600,8 +618,8 @@ if ($max === 0) { // Argument passed but not expected. return PEAR::raiseError('Argument '.$optname.' does not take any value', - CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, - null, 'Console_Getargs_Options::setValue()'); + CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, + null, 'Console_Getargs_Options::setValue()'); } // Not enough arguments passed for this option. return PEAR::raiseError('Argument '.$optname.' expects more than one value', @@ -626,9 +644,16 @@ // Argument is a switch if (isset($this->args[$pos+1]) && $this->isValue($this->args[$pos+1])) { // What we thought was the next option was really an argument for this option. - return PEAR::raiseError('Argument '.$optname.' does not take any value', - CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, - null, 'Console_Getargs_Options::setValue()'); + // First update the value + $this->updateValue($optname, true); + // Then try to assign values to parameters. + if (isset($this->_config['parameters'])) { + return $this->setValue('parameters', '', ++$pos); + } else { + return PEAR::raiseError('Argument '.$optname.' does not take any value', + CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, + null, 'Console_Getargs_Options::setValue()'); + } } // Set the switch to on. $this->updateValue($optname, true); @@ -661,24 +686,39 @@ // Argument takes one or more values $added = 0; + // If trying to assign values to parameters, must go back one position. + if ($optname == 'parameters') { + $pos = max($pos - 1, -1); + } for ($i = $pos + 1; $i <= count($this->args); $i++) { if (isset($this->args[$i]) && $this->isValue($this->args[$i])) { // Add the argument value until the next option is hit. $this->updateValue($optname, $this->args[$i]); $added++; $pos++; - continue; + // Only keep trying if we haven't filled up yet. + if ($added < $max) { + continue; + } } if ($min > $added) { // There aren't enough arguments for this option. return PEAR::raiseError('Argument '.$optname.' expects at least '.$min.(($min > 1) ? ' values' : ' value'), CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, null, 'Console_Getargs_Options::setValue()'); - } else if ($max !== -1 && $added > $max) { + } else if ($max !== -1 && $added >= $max) { // Too many arguments for this option. - return PEAR::raiseError('Argument '.$optname.' expects maximum '.$max.' values', - CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, - null, 'Console_Getargs_Options::setValue()'); + // Try to add the extra options to parameters. + if (isset($this->_config['parameters']) && $optname != 'parameters') { + return $this->setValue('parameters', '', ++$pos); + } elseif ($optname == 'parameters' && !isset($this->args[$i + 1])) { + $pos += $added; + break; + } else { + return PEAR::raiseError('Argument '.$optname.' expects maximum '.$max.' values', + CONSOLE_GETARGS_ERROR_USER, PEAR_ERROR_RETURN, + null, 'Console_Getargs_Options::setValue()'); + } } break; } http://cvs.php.net/diff.php/pear/Console_Getargs/examples/example.php?r1=1.3&r2=1.4&ty=u Index: pear/Console_Getargs/examples/example.php diff -u pear/Console_Getargs/examples/example.php:1.3 pear/Console_Getargs/examples/example.php:1.4 --- pear/Console_Getargs/examples/example.php:1.3 Thu Sep 30 12:37:27 2004 +++ pear/Console_Getargs/examples/example.php Tue Oct 5 16:59:41 2004 @@ -2,10 +2,10 @@ /** * Example for Console_Getargs class * - * $Id: example.php,v 1.3 2004/09/30 16:37:27 scottmattocks Exp $ + * $Id: example.php,v 1.4 2004/10/05 20:59:41 scottmattocks Exp $ */ -require_once('Console/Getargs.php'); +require_once('../Getargs.php'); $config = array( // Option takes 2 values @@ -19,7 +19,9 @@ // Option takes from 1 to an unlimited number of values 'filters' => array('short' => 'fi', 'min' => 1, 'max' => -1, 'desc' => 'Set the filters to be applied to the image upon conversion. The filters will be used in the order they are set.'), // Option accept 1 value or nothing. If nothing, then the default value is used - 'verbose' => array('short' => 'v', 'min' => 0, 'max' => 1, 'desc' => 'Set the verbose level.', 'default' => 3) + 'verbose' => array('short' => 'v', 'min' => 0, 'max' => 1, 'desc' => 'Set the verbose level.', 'default' => 3), + // Parameters. Anything leftover at the end of the command line is added. + 'parameters' => array('min' => 0, 'max' => 2, 'desc' => 'Set the application parameters.', 'default' => 'foo1') ); $args =& Console_Getargs::factory($config); @@ -45,15 +47,35 @@ } echo 'Width: '.$args->getValue('w')."\n"; echo 'Debug: '.($args->isDefined('d') ? "YES\n" : "NO\n"); +echo 'Parameters: '.($args->isDefined('parameters') ? is_array($args->getValue('parameters')) ? implode(', ', $args->getValue('parameters')) : $args->getValue('parameters') : "undefined") . "\n"; // Test with: // ---------- +// Get the help message // php -q example.php -h +// +// Pass two files // php -q example.php -v -f src.tiff dest.tiff +// +// Set verbose level 5, pass two files, and debug // php -q example.php -v5 -f src.tiff dest.tiff -d +// +// Set verbose level 1, pass two files, debug, and set width to 100 // php -q example.php -v 1 -f src.tiff dest.tiff -d --width=100 +// +// Set verbose (defaults to 3), pass two files, and pass two filters // php -q example.php -v -f src.tiff dest.tiff -fi sharp blur +// +// Set three formats // php -q example.php --format gif jpeg png +// +// Debug, set verbose to default level and width to 100 // php -q example.php -dvw 100 +// +// Pass two application parameters +// php -q example.php foo1 foo2 +// +// Debug, set verbose level 5, pass two files, and pass two application parameters +// php -q examples/example.php -dv5 -f src.tiff dest.tiff foo1 foo2 ?>