PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/antlion/classes/CommandLineOption.php

https://code.google.com/p/overwatch/
PHP | 53 lines | 47 code | 2 blank | 4 comment | 8 complexity | f46041062c355ba90ba19a5774603df9 MD5 | raw file
  1. <?php class CommandLineOption {
  2. protected $Default; // the default value for the option
  3. protected $Usage; // the human-readable string with the option's short description
  4. protected $Value; // the value of the option
  5. protected $Explicit = FALSE; // whether the option was set explicitely or was it set from default
  6. protected $GetOpt = '::'; // which getopt () modifier to use
  7. protected function Value () {
  8. if ($this->Explicit) {
  9. return ($this->Value);
  10. } else {
  11. return ($this->Default);
  12. }
  13. }
  14. protected function Usage () {
  15. if (strlen ($this->Default)) {
  16. $Default = '"'.$this->Default.'"';
  17. } else {
  18. $Default = 'empty';
  19. }
  20. return ($this->Usage." (default: $Default)");
  21. }
  22. public function __construct ($Default, $Usage) {
  23. $this->Default = $Default;
  24. $this->Usage = $Usage;
  25. }
  26. public function __get ($Property) {
  27. switch ($Property) {
  28. case 'Explicit': return ($this->Explicit);
  29. case 'GetOpt': return ($this->GetOpt);
  30. case 'Usage': return ($this->Usage ());
  31. case 'Value': return ($this->Value ());
  32. default: return (NULL);
  33. }
  34. }
  35. // one caveat below
  36. // consider a command line '$0 -a -b' with 'a' option taking a value. as you can see in $this->GetOpt the value is always optional
  37. // (if it isn't, '-b' will be parsed as a value for '-a', which will break all the next args)
  38. // so the value for '-a' will be FALSE, which is equivalent to not setting it at all.
  39. public function Set ($Value) {
  40. $this->Explicit = TRUE;
  41. if (is_array ($Value)) {
  42. $Value = end ($Value);
  43. }
  44. if (FALSE === $Value) {
  45. $this->Explicit = FALSE;
  46. } else {
  47. $this->Value = $Value;
  48. }
  49. return ($this->Value);
  50. }
  51. } ?>