Ejemplo n.º 1
0
 /**
  * Decodes a set of options from any number of sources
  * Options can come in from the command line or via the request global.
  * This function find where they are located and pulls them in. When
  * variables can't be found, the defaults in $request are used.
  * @param $request the default variables to pull in
  * @return array
  **/
 public function getLongOptions($request)
 {
     $sequentials = array();
     $arguments = array();
     // get our arguments off of $_REQUEST in CGI mode
     if (SNAP_CGI_MODE) {
         foreach ($_REQUEST as $key => $value) {
             if ($value === "") {
                 // a value of "" means it had no =, sequential
                 $sequentials[] = Snap_Request::unmangleRequest($key);
             } else {
                 // standard key/value pair
                 $key = Snap_Request::unmangleRequest($key);
                 $key = preg_replace('/^--(.*)/', '$1', $key);
                 $arguments[$key] = Snap_Request::unmangleRequest($value);
             }
         }
     } else {
         // CLI mode
         global $argv;
         if (!is_array($argv)) {
             break;
         }
         $prev = '';
         foreach ($argv as $idx => $arg) {
             if ($idx == 0) {
                 continue;
             }
             // if there is a \ as last char, link to $prev and go again
             if (strrpos($arg, '\\') + 1 === strlen($arg)) {
                 $prev = $arg . ' ';
                 continue;
             }
             $arg = $prev . $arg;
             $prev = '';
             if (strpos($arg, '--') === FALSE) {
                 $sequentials[] = $arg;
                 continue;
             }
             $opt_pair = explode('=', substr($arg, 2), 2);
             $opt_name = $opt_pair[0];
             $opt_value = isset($opt_pair[1]) ? $opt_pair[1] : TRUE;
             $arguments[$opt_name] = trim($opt_value, '"\'');
         }
     }
     foreach ($sequentials as $idx => $arg) {
         $arguments[$idx] = $arg;
     }
     // now, satisfy out output
     foreach ($request as $key => $default) {
         if (isset($arguments[$key]) && $arguments[$key]) {
             $request[$key] = $arguments[$key];
             continue;
         }
         if (isset($arguments[$key]) && is_bool($default)) {
             $request[$key] = TRUE;
             continue;
         }
     }
     return $request;
 }