@@ -392,6 +392,8 @@ public function parse() {
392392 $ this ->_parsed = array ();
393393 $ this ->_lexer = new Lexer ($ this ->_input );
394394
395+ $ this ->_applyDefaults ();
396+
395397 foreach ($ this ->_lexer as $ argument ) {
396398 if ($ this ->_parseFlag ($ argument )) {
397399 continue ;
@@ -408,6 +410,24 @@ public function parse() {
408410 }
409411 }
410412
413+ /**
414+ * This applies the default values, if any, of all of the
415+ * flags and options, so that if there is a default value
416+ * it will be available.
417+ */
418+ private function _applyDefaults () {
419+ foreach ($ this ->_flags as $ flag => $ settings ) {
420+ $ this [$ flag ] = $ settings ['default ' ];
421+ }
422+
423+ foreach ($ this ->_options as $ option => $ settings ) {
424+ // If the default is 0 we should still let it be set.
425+ if (!empty ($ settings ['default ' ]) || $ settings ['default ' ] === 0 ) {
426+ $ this [$ option ] = $ settings ['default ' ];
427+ }
428+ }
429+ }
430+
411431 private function _warn ($ message ) {
412432 trigger_error ('[ ' . __CLASS__ .'] ' . $ message , E_USER_WARNING );
413433 }
@@ -439,7 +459,7 @@ private function _parseOption($option) {
439459 if ($ this ->_lexer ->end () || !$ this ->_lexer ->peek ->isValue ) {
440460 $ optionSettings = $ this ->getOption ($ option ->key );
441461
442- if (empty ($ optionSettings ['default ' ])) {
462+ if (empty ($ optionSettings ['default ' ]) && $ optionSettings !== 0 ) {
443463 // Oops! Got no value and no default , throw a warning and continue.
444464 $ this ->_warn ('no value given for ' . $ option ->raw );
445465 $ this [$ option ->key ] = null ;
@@ -466,4 +486,3 @@ private function _parseOption($option) {
466486 return true ;
467487 }
468488}
469-
0 commit comments