Browse Source

Checking if short options exist, fixed an warning caused by not checking it.

Renan Gonçalves 14 years ago
parent
commit
7ae89c3679

+ 3 - 0
lib/Cake/Console/ConsoleOptionParser.php

@@ -561,6 +561,9 @@ class ConsoleOptionParser {
 				array_unshift($this->_tokens, '-' . $flags[$i]);
 			}
 		}
+		if (!isset($this->_shortOptions[$key])) {
+			throw new ConsoleException(__d('cake_console', 'Unknown short option `%s`', $key));
+		}
 		$name = $this->_shortOptions[$key];
 		return $this->_parseOption($name, $params);
 	}

+ 12 - 0
lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php

@@ -249,6 +249,18 @@ class ConsoleOptionParserTest extends CakeTestCase {
 
 		$result = $parser->parse(array('--fail', 'other'));
 	}
+	
+/**
+ * test parsing short options that do not exist.
+ *
+ * @expectedException ConsoleException
+ */
+	public function testShortOptionThatDoesNotExist() {
+		$parser = new ConsoleOptionParser('test', false);
+		$parser->addOption('no-commit', array('boolean' => true));
+
+		$result = $parser->parse(array('-f'));
+	}
 
 /**
  * test that options with choices enforce them.