Browse Source

Add methods to interact with options in Arguments objects.

Refs #11137
Mark Story 8 years ago
parent
commit
0390dbddbe
2 changed files with 94 additions and 0 deletions
  1. 36 0
      src/Console/Arguments.php
  2. 58 0
      tests/TestCase/Console/ArgumentsTest.php

+ 36 - 0
src/Console/Arguments.php

@@ -123,4 +123,40 @@ class Arguments
 
         return $this->args[$offset];
     }
+
+    /**
+     * Get an array of all the options
+     *
+     * @return array
+     */
+    public function getOptions()
+    {
+        return $this->options;
+    }
+
+    /**
+     * Get an option's value or null
+     *
+     * @param string $name The name of the option to check.
+     * @return string|int|bool|null The option value or null.
+     */
+    public function getOption($name)
+    {
+        if (isset($this->options[$name])) {
+            return $this->options[$name];
+        }
+
+        return null;
+    }
+
+    /**
+     * Check if an option is defined and not null.
+     *
+     * @param string $name The name of the option to check.
+     * @return bool
+     */
+    public function hasOption($name)
+    {
+        return isset($this->options[$name]);
+    }
 }

+ 58 - 0
tests/TestCase/Console/ArgumentsTest.php

@@ -95,4 +95,62 @@ class ArgumentsTest extends TestCase
         $this->assertNull($args->getArgument('Color'));
         $this->assertNull($args->getArgument('hair'));
     }
+
+    /**
+     * test getOptions()
+     *
+     * @return void
+     */
+    public function testGetOptions()
+    {
+        $options = [
+            'verbose' => true,
+            'off' => false,
+            'empty' => ''
+        ];
+        $args = new Arguments([], $options, []);
+        $this->assertSame($options, $args->getOptions());
+    }
+
+    /**
+     * test hasOption()
+     *
+     * @return void
+     */
+    public function testHasOption()
+    {
+        $options = [
+            'verbose' => true,
+            'off' => false,
+            'zero' => 0,
+            'empty' => ''
+        ];
+        $args = new Arguments([], $options, []);
+        $this->assertTrue($args->hasOption('verbose'));
+        $this->assertTrue($args->hasOption('off'));
+        $this->assertTrue($args->hasOption('empty'));
+        $this->assertTrue($args->hasOption('zero'));
+        $this->assertFalse($args->hasOption('undef'));
+    }
+
+    /**
+     * test getOption()
+     *
+     * @return void
+     */
+    public function testGetOption()
+    {
+        $options = [
+            'verbose' => true,
+            'off' => false,
+            'zero' => 0,
+            'empty' => ''
+        ];
+        $args = new Arguments([], $options, []);
+        $this->assertTrue($args->getOption('verbose'));
+        $this->assertFalse($args->getOption('off'));
+        $this->assertSame('', $args->getOption('empty'));
+        $this->assertSame(0, $args->getOption('zero'));
+        $this->assertNull($args->getOption('undef'));
+    }
 }