[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/ -> clilib.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Command line utility functions and classes
  20   *
  21   * @package    core
  22   * @subpackage cli
  23   * @copyright  2009 Petr Skoda (http://skodak.org)
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  // NOTE: no MOODLE_INTERNAL test here, sometimes we use this before requiring Moodle libs!
  28  
  29  /**
  30   * Get input from user
  31   * @param string $prompt text prompt, should include possible options
  32   * @param string $default default value when enter pressed
  33   * @param array $options list of allowed options, empty means any text
  34   * @param bool $casesensitive true if options are case sensitive
  35   * @return string entered text
  36   */
  37  function cli_input($prompt, $default='', array $options=null, $casesensitiveoptions=false) {
  38      echo $prompt;
  39      echo "\n: ";
  40      $input = fread(STDIN, 2048);
  41      $input = trim($input);
  42      if ($input === '') {
  43          $input = $default;
  44      }
  45      if ($options) {
  46          if (!$casesensitiveoptions) {
  47              $input = strtolower($input);
  48          }
  49          if (!in_array($input, $options)) {
  50              echo "Incorrect value, please retry.\n"; // TODO: localize, mark as needed in install
  51              return cli_input($prompt, $default, $options, $casesensitiveoptions);
  52          }
  53      }
  54      return $input;
  55  }
  56  
  57  /**
  58   * Returns cli script parameters.
  59   * @param array $longoptions array of --style options ex:('verbose'=>false)
  60   * @param array $shortmapping array describing mapping of short to long style options ex:('h'=>'help', 'v'=>'verbose')
  61   * @return array array of arrays, options, unrecognised as optionlongname=>value
  62   */
  63  function cli_get_params(array $longoptions, array $shortmapping=null) {
  64      $shortmapping = (array)$shortmapping;
  65      $options      = array();
  66      $unrecognized = array();
  67  
  68      if (empty($_SERVER['argv'])) {
  69          // bad luck, we can continue in interactive mode ;-)
  70          return array($options, $unrecognized);
  71      }
  72      $rawoptions = $_SERVER['argv'];
  73  
  74      //remove anything after '--', options can not be there
  75      if (($key = array_search('--', $rawoptions)) !== false) {
  76          $rawoptions = array_slice($rawoptions, 0, $key);
  77      }
  78  
  79      //remove script
  80      unset($rawoptions[0]);
  81      foreach ($rawoptions as $raw) {
  82          if (substr($raw, 0, 2) === '--') {
  83              $value = substr($raw, 2);
  84              $parts = explode('=', $value);
  85              if (count($parts) == 1) {
  86                  $key   = reset($parts);
  87                  $value = true;
  88              } else {
  89                  $key = array_shift($parts);
  90                  $value = implode('=', $parts);
  91              }
  92              if (array_key_exists($key, $longoptions)) {
  93                  $options[$key] = $value;
  94              } else {
  95                  $unrecognized[] = $raw;
  96              }
  97  
  98          } else if (substr($raw, 0, 1) === '-') {
  99              $value = substr($raw, 1);
 100              $parts = explode('=', $value);
 101              if (count($parts) == 1) {
 102                  $key   = reset($parts);
 103                  $value = true;
 104              } else {
 105                  $key = array_shift($parts);
 106                  $value = implode('=', $parts);
 107              }
 108              if (array_key_exists($key, $shortmapping)) {
 109                  $options[$shortmapping[$key]] = $value;
 110              } else {
 111                  $unrecognized[] = $raw;
 112              }
 113          } else {
 114              $unrecognized[] = $raw;
 115              continue;
 116          }
 117      }
 118      //apply defaults
 119      foreach ($longoptions as $key=>$default) {
 120          if (!array_key_exists($key, $options)) {
 121              $options[$key] = $default;
 122          }
 123      }
 124      // finished
 125      return array($options, $unrecognized);
 126  }
 127  
 128  /**
 129   * Print or return section separator string
 130   * @param bool $return false means print, true return as string
 131   * @return mixed void or string
 132   */
 133  function cli_separator($return=false) {
 134      $separator = str_repeat('-', 79)."\n";
 135      if ($return) {
 136          return $separator;
 137      } else {
 138          echo $separator;
 139      }
 140  }
 141  
 142  /**
 143   * Print or return section heading string
 144   * @param string $string text
 145   * @param bool $return false means print, true return as string
 146   * @return mixed void or string
 147   */
 148  function cli_heading($string, $return=false) {
 149      $string = "== $string ==\n";
 150      if ($return) {
 151          return $string;
 152      } else {
 153          echo $string;
 154      }
 155  }
 156  
 157  /**
 158   * Write error notification
 159   * @param $text
 160   * @return void
 161   */
 162  function cli_problem($text) {
 163      fwrite(STDERR, $text."\n");
 164  }
 165  
 166  /**
 167   * Write to standard out and error with exit in error.
 168   *
 169   * @param string $text
 170   * @param int $errorcode
 171   * @return void (does not return)
 172   */
 173  function cli_error($text, $errorcode=1) {
 174      fwrite(STDERR, $text);
 175      fwrite(STDERR, "\n");
 176      die($errorcode);
 177  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1