Browse Source

Add ConsoleOptionParser::removeOption()

When building option parsers with inheritance it is sometimes useful to
remove options that the parent class added.
mark_story 12 years ago
parent
commit
1b24fcdef8

+ 11 - 0
src/Console/ConsoleOptionParser.php

@@ -304,6 +304,17 @@ class ConsoleOptionParser {
 	}
 
 /**
+ * Remove an option from the option parser.
+ *
+ * @param string $name The option name to remove.
+ * @return ConsoleOptionParser $this
+ */
+	public function removeOption($name) {
+		unset($this->_options[$name]);
+		return $this;
+	}
+
+/**
  * Add a positional argument to the option parser.
  *
  * ### Params

+ 14 - 2
tests/TestCase/Console/ConsoleOptionParserTest.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * ConsoleOptionParserTest file
- *
  * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -72,6 +70,20 @@ class ConsoleOptionParserTest extends TestCase {
 	}
 
 /**
+ * test removing an option
+ *
+ * @return void
+ */
+	public function testRemoveOption() {
+		$parser = new ConsoleOptionParser('test', false);
+		$result = $parser->addOption('test')
+			->removeOption('test')
+			->removeOption('help');
+		$this->assertSame($parser, $result, 'Did not return $this from removeOption');
+		$this->assertEquals([], $result->options());
+	}
+
+/**
  * test adding an option and using the long value for parsing.
  *
  * @return void