console.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php namespace Laravel\CLI;
  2. class Console {
  3. /**
  4. * Parse the command line arguments and return the results.
  5. *
  6. * @param array $argv
  7. * @param array
  8. */
  9. public static function options($argv)
  10. {
  11. $options = array();
  12. $arguments = array();
  13. for ($i = 0, $count = count($argv); $i < $count; $i++)
  14. {
  15. $argument = $argv[$i];
  16. // If the CLI argument starts with a double hyphen, it is an option,
  17. // so we will extract the value and add it to the array of options
  18. // to be returned by the method.
  19. if (starts_with($argument, '--'))
  20. {
  21. // By default, we will assume the value of the options is true,
  22. // but if the option contains an equals sign, we will take the
  23. // value to the right of the equals sign as the value and
  24. // remove the value from the option key.
  25. list($key, $value) = array(substr($argument, 2), true);
  26. if (($equals = strpos($argument, '=')) !== false)
  27. {
  28. $key = substr($argument, 2, $equals - 2);
  29. $value = substr($argument, $equals + 1);
  30. }
  31. $options[$key] = $value;
  32. }
  33. // If the CLI argument does not start with a double hyphen it is
  34. // simply an argument to be passed to the console task so we'll
  35. // add it to the array of "regular" arguments.
  36. else
  37. {
  38. $arguments[] = $argument;
  39. }
  40. }
  41. return array($arguments, $options);
  42. }
  43. }