Overview

Namespaces

  • None
  • Peg
    • Cli
      • Commands
    • Config
    • Custom
      • Command
        • Action
          • Generate
          • Parse
      • CommandLine
      • Config
      • Localization
      • Utilities
    • Lib
      • Definitions
        • Element
      • Generator
      • Lexers
      • Plugins
      • Signals
        • Data
          • Definitions
          • Lexers
        • Type
  • PHP

Classes

  • Action
  • Command
  • Error
  • Option
  • OptionType
  • Parser
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: /**
  3:  * @author Jefferson González
  4:  * @license MIT
  5:  * @link http://github.com/peg-org/peg-custom Source code.
  6:  */
  7: 
  8: namespace Peg\Custom\CommandLine;
  9: 
 10: /**
 11:  * Represents a command line option.
 12:  */
 13: class Option
 14: {
 15: 
 16:     /**
 17:      * The human readable name of the option. Example: --use-something stored
 18:      * on this variable as use-something.
 19:      * @var string
 20:      */
 21:     public $long_name;
 22: 
 23:     /**
 24:      * A one letter case-sensitive alias for the option. Example: -u stored on
 25:      * this variable as u.
 26:      * @var string
 27:      */
 28:     public $short_name;
 29: 
 30:     /**
 31:      * Data type of the option represented by one of the constants from
 32:      * \Peg\Custom\CommandLine\OptionType
 33:      * @var integer
 34:      */
 35:     public $type;
 36: 
 37:     /**
 38:      * Current value of the option.
 39:      * @var integer|string
 40:      */
 41:     public $value;
 42: 
 43:     /**
 44:      * A default value used in case the user didn't provided one and the option
 45:      * isn't marked as required.
 46:      * @var integer|string
 47:      */
 48:     public $default_value;
 49: 
 50:     /**
 51:      * Indicates if an option of type FLAG was passed on the command line.
 52:      * @var boolean
 53:      */
 54:     public $active;
 55: 
 56:     /**
 57:      * Flag that indicates if the option is required or not.
 58:      * @var boolean
 59:      */
 60:     public $required;
 61: 
 62:     /**
 63:      * Information displayed when the application is executed without
 64:      * options or with: -h, --help
 65:      * @var string
 66:      */
 67:     public $description;
 68: 
 69:     /**
 70:      * Initilize the option with an optional list of properties defined
 71:      * on an array.
 72:      * @param array $properties
 73:      */
 74:     public function __construct($properties = null)
 75:     {
 76:         if(is_array($properties) && count($properties) == 6)
 77:         {
 78:             $this->long_name = $properties["long_name"];
 79:             $this->short_name = $properties["short_name"];
 80:             $this->type = $properties["type"] ? $properties["type"] : OptionType::STRING;
 81:             $this->default_value = $properties["default_value"] ? $properties["default_value"] : "";
 82:             $this->required = $properties["required"];
 83:             $this->description = $properties["description"];
 84:         }
 85:         else
 86:         {
 87:             $this->type = OptionType::STRING;
 88:             $this->default_value = "";
 89:         }
 90:     }
 91: 
 92:     /**
 93:      * Checks if the option value is valid.
 94:      * @return boolean
 95:      */
 96:     public function IsValid()
 97:     {
 98:         if($this->required && !isset($this->value))
 99:             return false;
100: 
101:         // Check with user given value
102:         if(isset($this->value))
103:         {
104:             if($this->type == OptionType::INTEGER && is_int($this->value))
105:                 return true;
106: 
107:             if($this->type == OptionType::STRING && is_string($this->value))
108:                 return true;
109:         }
110: 
111:         // Check with default value
112:         if($this->type == OptionType::INTEGER && is_int($this->default_value))
113:             return true;
114: 
115:         if($this->type == OptionType::STRING && is_string($this->default_value))
116:             return true;
117: 
118:         // Check if flag
119:         if($this->type == OptionType::FLAG && $this->active)
120:             return true;
121: 
122:         return false;
123:     }
124: 
125:     /**
126:      * Automatically returns the current or default value or null if neither
127:      * is set.
128:      * @return int|string
129:      */
130:     public function GetValue()
131:     {
132:         if(isset($this->value))
133:             return $this->value;
134: 
135:         elseif(isset($this->default_value))
136:             return $this->default_value;
137: 
138:         return null;
139:     }
140: 
141:     /**
142:      * Useful to add a value while checking if the value isn't in fact another
143:      * parameter like --other -o
144:      * @param integer|string $value
145:      * @return boolean True if value was set otherwise false.
146:      */
147:     public function SetValue($value)
148:     {
149:         if(ltrim($value, "-") == $value)
150:         {
151:             if(
152:                     $this->type == OptionType::STRING &&
153:                     $this->required &&
154:                     strlen($value) < 1
155:             )
156:                 return false;
157: 
158:             $this->value = $value;
159:             return true;
160:         }
161: 
162:         return false;
163:     }
164: 
165: }
PEG Api API documentation generated by ApiGen 2.8.0