/antlion/classes/CommandLineOption.php
PHP | 53 lines | 47 code | 2 blank | 4 comment | 8 complexity | f46041062c355ba90ba19a5774603df9 MD5 | raw file
- <?php class CommandLineOption {
- protected $Default; // the default value for the option
- protected $Usage; // the human-readable string with the option's short description
- protected $Value; // the value of the option
- protected $Explicit = FALSE; // whether the option was set explicitely or was it set from default
- protected $GetOpt = '::'; // which getopt () modifier to use
- protected function Value () {
- if ($this->Explicit) {
- return ($this->Value);
- } else {
- return ($this->Default);
- }
- }
- protected function Usage () {
- if (strlen ($this->Default)) {
- $Default = '"'.$this->Default.'"';
- } else {
- $Default = 'empty';
- }
- return ($this->Usage." (default: $Default)");
- }
- public function __construct ($Default, $Usage) {
- $this->Default = $Default;
- $this->Usage = $Usage;
- }
- public function __get ($Property) {
- switch ($Property) {
- case 'Explicit': return ($this->Explicit);
- case 'GetOpt': return ($this->GetOpt);
- case 'Usage': return ($this->Usage ());
- case 'Value': return ($this->Value ());
- default: return (NULL);
- }
- }
- // one caveat below
- // 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
- // (if it isn't, '-b' will be parsed as a value for '-a', which will break all the next args)
- // so the value for '-a' will be FALSE, which is equivalent to not setting it at all.
- public function Set ($Value) {
- $this->Explicit = TRUE;
- if (is_array ($Value)) {
- $Value = end ($Value);
- }
- if (FALSE === $Value) {
- $this->Explicit = FALSE;
- } else {
- $this->Value = $Value;
- }
- return ($this->Value);
- }
- } ?>