Source for file jCmdUtils.class.php

Documentation is available at jCmdUtils.class.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage utils
  5. * @author     Loic Mathaud
  6. * @contributor Laurent Jouanneau
  7. * @copyright  2006 Loic Mathaud, 2008 Laurent Jouanneau
  8. * @link        http://www.jelix.org
  9. * @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  10. */
  11.  
  12. /**
  13.  * utilities functions for command line
  14.  * @package    jelix
  15.  * @subpackage utils
  16.  * @static
  17.  */
  18. class jCmdUtils {
  19.  
  20.     private function __construct() {}
  21.  
  22.     /**
  23.      * analyze command line parameters and return an array
  24.      * of all options and parameters which correspond to
  25.      * allowed options and parameters
  26.      *
  27.      * allowed options should be an array like this :
  28.      * array('-option1'=>bool, '-option2'=>bool, ..)
  29.      * the boolean indicates that the option has a parameter on the CLI
  30.      *
  31.      * allowed parameters is an array like this:
  32.      * array('param1'=>bool, 'param2'=>bool, ..)
  33.      * it means that the first parameter value will be in the param1,
  34.      * the second in param2 etc.. The boolean says that the parameter
  35.      * is optional. If a parameter is optional, following parameters
  36.      * should be optional.
  37.      *
  38.      * the returned array contains two array :
  39.      * array('-option1'=>value, '-option2'=>value, ...)
  40.      * array('param1'=>value, 'param2'=>value...)
  41.      *
  42.      *
  43.      * @param array $argv the array of parameters given by php-cli
  44.      * @param array $sws allowed options
  45.      * @param array $params allowed parameters
  46.      * @return array an array with the array of founded option and
  47.      *                         an array with founded parameters
  48.      */
  49.     public static function getOptionsAndParams($argv, $sws, $params) {
  50.         $switches = array();
  51.         $parameters = array();
  52.  
  53.         //---------- get the switches
  54.         while (count($argv) && $argv[0]{0} == '-') {
  55.             if (isset($sws[$argv[0]])) {
  56.                 if ($sws[$argv[0]]) {
  57.                     if (isset($argv[1]) && $argv[1]{0} != '-') {
  58.                         $sw = array_shift($argv);
  59.                         $switches[$sw] = array_shift($argv);
  60.                     } else {
  61.                         throw new jException('jelix~errors.cli.option.value.missing', $argv[0]);
  62.                     }
  63.                 } else {
  64.                     $sw = array_shift($argv);
  65.                     $switches[$sw] = true;
  66.                 }
  67.             } else {
  68.                 throw new jException('jelix~errors.cli.unknown.option', $argv[0]);
  69.             }
  70.         }
  71.  
  72.         //---------- get the parameters
  73.         foreach ($params as $pname => $needed) {
  74.             if (count($argv) == 0) {
  75.                 if ($needed) {
  76.                     throw new jException('jelix~errors.cli.param.missing', $pname);
  77.                 } else {
  78.                     break;
  79.                 }
  80.             }
  81.             $parameters[$pname]=array_shift($argv);
  82.         }
  83.  
  84.         if (count($argv)) {
  85.             throw new jException('jelix~errors.cli.two.many.parameters');
  86.         }
  87.  
  88.         return array($switches , $parameters);
  89.     }
  90.  
  91. }

Documentation generated on Mon, 19 Sep 2011 14:12:00 +0200 by phpDocumentor 1.4.3