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.  
  50.     public static function getOptionsAndParams($argv, $sws, $params) {
  51.         $switches = array();
  52.         $parameters = array();
  53.  
  54.         //---------- get the switches
  55.         while (count($argv) && $argv[0]{0} == '-') {
  56.             if (isset($sws[$argv[0]])) {
  57.                 if ($sws[$argv[0]]) {
  58.                     if (isset($argv[1]) && $argv[1]{0} != '-') {
  59.                         $sw = array_shift($argv);
  60.                         $switches[$sw] = array_shift($argv);
  61.                     } else {
  62.                         throw new jException('jelix~errors.cli.option.value.missing', $argv[0]);
  63.                     }
  64.                 } else {
  65.                     $sw = array_shift($argv);
  66.                     $switches[$sw] = true;
  67.                 }
  68.             } else {
  69.                 throw new jException('jelix~errors.cli.unknow.option', $argv[0]);
  70.             }
  71.         }
  72.  
  73.         //---------- get the parameters
  74.         foreach ($params as $pname => $needed) {
  75.             if (count($argv) == 0) {
  76.                 if ($needed) {
  77.                     throw new jException('jelix~errors.cli.param.missing', $pname);
  78.                 } else {
  79.                     break;
  80.                 }
  81.             }
  82.             $parameters[$pname]=array_shift($argv);
  83.         }
  84.  
  85.         if (count($argv)) {
  86.             throw new jException('jelix~errors.cli.two.many.parameters');
  87.         }
  88.  
  89.         return array($switches , $parameters);
  90.     }
  91.  
  92. }
  93.  
  94. ?>

Documentation generated on Wed, 07 Sep 2011 13:46:45 +0200 by phpDocumentor 1.4.3