Esempio n. 1
0
 public static function parseArgv($argv = null)
 {
     $argv = $argv !== null ? $argv : $_SERVER['argv'];
     $out = [0 => array_shift($argv)];
     $countArgs = count($argv);
     for ($i = 0, $j = $countArgs; $i < $j; $i++) {
         $arg = $argv[$i];
         if (substr($arg, 0, 2) === '--') {
             // --foo --bar=baz
             $eqPos = strpos($arg, '=');
             if ($eqPos === false) {
                 // --foo
                 $key = substr($arg, 0);
                 if ($i + 1 < $j && $argv[$i + 1][0] !== '-') {
                     // --foo value
                     $value = $argv[$i + 1];
                     $i++;
                 } else {
                     $value = isset($out[$key]) ? $out[$key] : true;
                 }
                 $out[$key] = $value;
             } else {
                 // --bar=baz
                 $key = substr($arg, 0, $eqPos);
                 $value = substr($arg, $eqPos + 1);
                 $out[$key] = $value;
             }
         } else {
             // -k=value -abc
             if (substr($arg, 0, 1) === '-') {
                 if (substr($arg, 2, 1) === '=') {
                     // -k=value
                     $key = substr($arg, 0, 2);
                     $value = substr($arg, 3);
                     $out[$key] = $value;
                 } else {
                     // -abc
                     $chars = str_split(substr($arg, 1));
                     foreach ($chars as $char) {
                         $key = '-' . $char;
                         $value = isset($out[$key]) ? $out[$key] : true;
                         $out[$key] = $value;
                     }
                     if ($i + 1 < $j && $argv[$i + 1][0] !== '-') {
                         // -a value1 -abc value2
                         $out[$key] = $argv[$i + 1];
                         $i++;
                     }
                 }
             } else {
                 // plain-arg
                 $value = $arg;
                 $out[] = $value;
             }
         }
     }
     self::$args = $out;
     return $out;
 }
Esempio n. 2
0
 /**
  * PARSE ARGUMENTS
  *
  * This command line option parser supports any combination of three types of options
  * [single character options (`-a -b` or `-ab` or `-c -d=dog` or `-cd dog`),
  * long options (`--foo` or `--bar=baz` or `--bar baz`)
  * and arguments (`arg1 arg2`)] and returns a simple array.
  *
  * [pfisher ~]$ php test.php --foo --bar=baz --spam eggs
  *   ["foo"]   => true
  *   ["bar"]   => "baz"
  *   ["spam"]  => "eggs"
  *
  * [pfisher ~]$ php test.php -abc foo
  *   ["a"]     => true
  *   ["b"]     => true
  *   ["c"]     => "foo"
  *
  * [pfisher ~]$ php test.php arg1 arg2 arg3
  *   [0]       => "arg1"
  *   [1]       => "arg2"
  *   [2]       => "arg3"
  *
  * [pfisher ~]$ php test.php plain-arg --foo --bar=baz --funny="spam=eggs" --also-funny=spam=eggs \
  * > 'plain arg 2' -abc -k=value "plain arg 3" --s="original" --s='overwrite' --s
  *   [0]       => "plain-arg"
  *   ["foo"]   => true
  *   ["bar"]   => "baz"
  *   ["funny"] => "spam=eggs"
  *   ["also-funny"]=> "spam=eggs"
  *   [1]       => "plain arg 2"
  *   ["a"]     => true
  *   ["b"]     => true
  *   ["c"]     => true
  *   ["k"]     => "value"
  *   [2]       => "plain arg 3"
  *   ["s"]     => "overwrite"
  *
  * Not supported: `-cd=dog`.
  *
  * @author              Patrick Fisher <*****@*****.**>
  * @since               August 21, 2009
  * @see                 https://github.com/pwfisher/CommandLine.php
  * @see                 http://www.php.net/manual/en/features.commandline.php
  *                      #81042 function arguments($argv) by technorati at gmail dot com, 12-Feb-2008
  *                      #78651 function getArgs($args) by B Crawford, 22-Oct-2007
  * @usage               $args = CommandLine::parseArgs($_SERVER['argv']);
  */
 public static function parseArgs($argv = null)
 {
     $argv = $argv ? $argv : $_SERVER['argv'];
     array_shift($argv);
     $out = array();
     for ($i = 0, $j = count($argv); $i < $j; $i++) {
         $arg = $argv[$i];
         // --foo --bar=baz
         if (substr($arg, 0, 2) === '--') {
             $eqPos = strpos($arg, '=');
             // --foo
             if ($eqPos === false) {
                 $key = substr($arg, 2);
                 // --foo value
                 if ($i + 1 < $j && $argv[$i + 1][0] !== '-') {
                     $value = $argv[$i + 1];
                     $i++;
                 } else {
                     $value = isset($out[$key]) ? $out[$key] : true;
                 }
                 $out[$key] = $value;
             } else {
                 $key = substr($arg, 2, $eqPos - 2);
                 $value = substr($arg, $eqPos + 1);
                 $out[$key] = $value;
             }
         } else {
             if (substr($arg, 0, 1) === '-') {
                 // -k=value
                 if (substr($arg, 2, 1) === '=') {
                     $key = substr($arg, 1, 1);
                     $value = substr($arg, 3);
                     $out[$key] = $value;
                 } else {
                     $chars = str_split(substr($arg, 1));
                     foreach ($chars as $char) {
                         $key = $char;
                         $value = isset($out[$key]) ? $out[$key] : true;
                         $out[$key] = $value;
                     }
                     // -a value1 -abc value2
                     if ($i + 1 < $j && $argv[$i + 1][0] !== '-') {
                         $out[$key] = $argv[$i + 1];
                         $i++;
                     }
                 }
             } else {
                 $value = $arg;
                 $out[] = $value;
             }
         }
     }
     self::$args = $out;
     return $out;
 }
Esempio n. 3
0
 /**
  * PARSE ARGUMENTS
  *
  * This command line option parser supports any combination of three types
  * of options (switches, flags and arguments) and returns a simple array.
  *
  * [pfisher ~]$ php test.php --foo --bar=baz
  *   ["foo"]   => true
  *   ["bar"]   => "baz"
  *
  * [pfisher ~]$ php test.php -abc
  *   ["a"]     => true
  *   ["b"]     => true
  *   ["c"]     => true
  *
  * [pfisher ~]$ php test.php arg1 arg2 arg3
  *   [0]       => "arg1"
  *   [1]       => "arg2"
  *   [2]       => "arg3"
  *
  * [pfisher ~]$ php test.php plain-arg --foo --bar=baz --funny="spam=eggs" --also-funny=spam=eggs \
  * > 'plain arg 2' -abc -k=value "plain arg 3" --s="original" --s='overwrite' --s
  *   [0]       => "plain-arg"
  *   ["foo"]   => true
  *   ["bar"]   => "baz"
  *   ["funny"] => "spam=eggs"
  *   ["also-funny"]=> "spam=eggs"
  *   [1]       => "plain arg 2"
  *   ["a"]     => true
  *   ["b"]     => true
  *   ["c"]     => true
  *   ["k"]     => "value"
  *   [2]       => "plain arg 3"
  *   ["s"]     => "overwrite"
  *
  * @author              Patrick Fisher <*****@*****.**>
  * @since               August 21, 2009
  * @see                 http://www.php.net/manual/en/features.commandline.php
  *                      #81042 function arguments($argv) by technorati at gmail dot com, 12-Feb-2008
  *                      #78651 function getArgs($args) by B Crawford, 22-Oct-2007
  * @usage               $args = CommandLine::parseArgs($_SERVER['argv']);
  */
 public static function parseArgs($argv)
 {
     array_shift($argv);
     $out = array();
     foreach ($argv as $arg) {
         // --foo --bar=baz
         if (substr($arg, 0, 2) == '--') {
             $eqPos = strpos($arg, '=');
             // --foo
             if ($eqPos === false) {
                 $key = substr($arg, 2);
                 $value = isset($out[$key]) ? $out[$key] : true;
                 $out[$key] = $value;
             } else {
                 $key = substr($arg, 2, $eqPos - 2);
                 $value = substr($arg, $eqPos + 1);
                 $out[$key] = $value;
             }
         } else {
             if (substr($arg, 0, 1) == '-') {
                 // -k=value
                 if (substr($arg, 2, 1) == '=') {
                     $key = substr($arg, 1, 1);
                     $value = substr($arg, 3);
                     $out[$key] = $value;
                 } else {
                     $chars = str_split(substr($arg, 1));
                     foreach ($chars as $char) {
                         $key = $char;
                         $value = isset($out[$key]) ? $out[$key] : true;
                         $out[$key] = $value;
                     }
                 }
             } else {
                 $value = $arg;
                 $out[] = $value;
             }
         }
     }
     self::$args = $out;
     return $out;
 }