@@ -98,22 +98,27 @@ public function epilog($text = null) {
98
98
* when this option is not present.
99
99
* - `description` - Description for this option.
100
100
* - `type` - Require a certain type. Available types are `int` and `string`. If the options
101
- * value is the wrong type an exception will be raised. Leave undefined to accept anything.
102
- * - `default` - The default value for this option. If not defined the default will be null .
101
+ * value is the wrong type an exception will be raised. Leave undefined to accept anything.
102
+ * - `default` - The default value for this option. If not defined the default will be true .
103
103
*
104
104
* @param string $name The long name you want to the value to be parsed out as when options are parsed.
105
105
* @param array $params An array of parameters that define the behavior of the option
106
106
* @return returns $this.
107
107
*/
108
108
public function addOption ($ name , $ params = array ()) {
109
109
$ defaults = array (
110
+ 'name ' => $ name ,
110
111
'shortcut ' => null ,
111
112
'required ' => false ,
112
113
'description ' => '' ,
113
114
'type ' => null ,
114
- 'default ' => null
115
+ 'default ' => true
115
116
);
116
- $ this ->_options [$ name ] = array_merge ($ defaults , $ params );
117
+ $ options = array_merge ($ defaults , $ params );
118
+ $ this ->_options [$ name ] = $ options ;
119
+ if (!empty ($ options ['shortcut ' ])) {
120
+ $ this ->_options [$ options ['shortcut ' ]] = $ options ;
121
+ }
117
122
return $ this ;
118
123
}
119
124
@@ -125,7 +130,67 @@ public function addOption($name, $params = array()) {
125
130
*/
126
131
public function parse ($ argv ) {
127
132
$ params = $ args = array ();
128
-
133
+ $ this ->_tokens = $ argv ;
134
+ while ($ token = array_shift ($ this ->_tokens )) {
135
+ if (substr ($ token , 0 , 2 ) == '-- ' ) {
136
+ $ params = $ this ->_parseLongOption ($ token , $ params );
137
+ } elseif (substr ($ token , 0 , 1 ) == '- ' ) {
138
+ $ params = $ this ->_parseShortOption ($ token , $ params );
139
+ }
140
+ }
129
141
return array ($ params , $ args );
130
142
}
143
+
144
+ /**
145
+ * Parse the value for a long option out of $this->_tokens
146
+ *
147
+ * @param string $option The option to parse.
148
+ * @param array $params The params to append the parsed value into
149
+ * @return array Params with $option added in.
150
+ */
151
+ protected function _parseLongOption ($ option , $ params ) {
152
+ $ name = substr ($ option , 2 );
153
+ return $ this ->_parseOptionName ($ name , $ params );
154
+ }
155
+
156
+ /**
157
+ * Parse the value for a short option out of $this->_tokens
158
+ *
159
+ * @param string $option The option to parse.
160
+ * @param array $params The params to append the parsed value into
161
+ * @return array Params with $option added in.
162
+ */
163
+ protected function _parseShortOption ($ option , $ params ) {
164
+ $ key = substr ($ option , 1 );
165
+ $ name = $ this ->_options [$ key ]['name ' ];
166
+ return $ this ->_parseOptionName ($ name , $ params );
167
+ }
168
+
169
+ /**
170
+ * Parse an option by its name index.
171
+ *
172
+ * @param string $option The option to parse.
173
+ * @param array $params The params to append the parsed value into
174
+ * @return array Params with $option added in.
175
+ */
176
+ protected function _parseOptionName ($ name , $ params ) {
177
+ $ definition = $ this ->_options [$ name ];
178
+ $ nextValue = $ this ->_nextToken ();
179
+ if (empty ($ nextValue )) {
180
+ $ value = $ definition ['default ' ];
181
+ } else if ($ nextValue {0 } != '- ' ) {
182
+ $ value = $ nextValue ;
183
+ }
184
+ $ params [$ name ] = $ value ;
185
+ return $ params ;
186
+ }
187
+
188
+ /**
189
+ * Find the next token in the argv set.
190
+ *
191
+ * @return next token or ''
192
+ */
193
+ protected function _nextToken () {
194
+ return isset ($ this ->_tokens [0 ]) ? $ this ->_tokens [0 ] : '' ;
195
+ }
131
196
}
0 commit comments