PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Argument.php

https://github.com/fabriceb/symfttpd
PHP | 48 lines | 29 code | 5 blank | 14 comment | 5 complexity | 8f9968d1b50c79c26c94b35ed83c8700 MD5 | raw file
  1. <?php
  2. if (version_compare(PHP_VERSION, '5.3.0') < 0)
  3. {
  4. require dirname(__FILE__).'/getopt.php';
  5. }
  6. class Argument
  7. {
  8. /**
  9. * Get command line options in a sane way.
  10. *
  11. * Examples:
  12. * getopt("p", "port", 42)
  13. * getopt("n", "nofork", false)
  14. * getopt("P", "path", null, true)
  15. * @param string $short Short option (one letter)
  16. * @param string $long Long option (many letters)
  17. * @param mixed $default "42". If strictly equal to false, the option \
  18. * is considered a boolean (option present returning true, false otherwise)
  19. * @param boolean $required Required option (ignored if $default is false)
  20. * @return string|boolean
  21. */
  22. static public function get($short, $long, $default, $required = false)
  23. {
  24. $addon = $default === false ? '' : ':';
  25. $options = (version_compare(PHP_VERSION, '5.3.0') < 0)
  26. ? _getopt($short.$addon, array($long.$addon))
  27. : getopt($short.$addon, array($long.$addon));
  28. $value = $default;
  29. if (isset($options[$short]))
  30. {
  31. $value = $default === false ? true : $options[$short];
  32. }
  33. if (isset($options[$long]))
  34. {
  35. $value = $default === false ? true : $options[$long];
  36. }
  37. if ($required && is_null($value))
  38. {
  39. throw new Exception('Missing required "'.$long.'" parameter');
  40. }
  41. return $value;
  42. }
  43. }