Browse Source

Add support for getting multiple and boolean option values

Corey Taylor 2 years ago
parent
commit
91d0980f7c
2 changed files with 56 additions and 7 deletions
  1. 36 7
      src/Console/Arguments.php
  2. 20 0
      tests/TestCase/Console/ArgumentsTest.php

+ 36 - 7
src/Console/Arguments.php

@@ -39,7 +39,7 @@ class Arguments
     /**
      * Named options
      *
-     * @var array<string, string|bool|null>
+     * @var array<string, list<string>|string|bool|null>
      */
     protected array $options;
 
@@ -84,7 +84,7 @@ class Arguments
     }
 
     /**
-     * Check if a positional argument exists
+     * Check if a positional argument exists by index
      *
      * @param int $index The argument index to check.
      * @return bool
@@ -111,7 +111,7 @@ class Arguments
     }
 
     /**
-     * Check if a positional argument exists by name
+     * Returns positional argument value by name or null if doesn't exist
      *
      * @param string $name The argument name to check.
      * @return string|null
@@ -129,7 +129,7 @@ class Arguments
     /**
      * Get an array of all the options
      *
-     * @return array<string, string|bool|null>
+     * @return array<string, list<string>|string|bool|null>
      */
     public function getOptions(): array
     {
@@ -137,14 +137,43 @@ class Arguments
     }
 
     /**
-     * Get an option's value or null
+     * Get a non-multiple option's value or null if not set.
      *
      * @param string $name The name of the option to check.
-     * @return string|bool|null The option value or null.
+     * @return string|bool|null
      */
     public function getOption(string $name): string|bool|null
     {
-        return $this->options[$name] ?? null;
+        $value = $this->options[$name] ?? null;
+        assert($value === null || is_string($value) || is_bool($value));
+
+        return $value;
+    }
+
+    /**
+     * Get a boolean option's value or null if not set.
+     *
+     * @return bool|null
+     */
+    public function getBooleanOption(string $name): ?bool
+    {
+        $value = $this->options[$name] ?? null;
+        assert($value === null || is_bool($value));
+
+        return $value;
+    }
+
+    /**
+     * Gets a multiple option's value or null if not set.
+     *
+     * @return list<string>|null
+     */
+    public function getMultipleOption(string $name): ?array
+    {
+        $value = $this->options[$name] ?? null;
+        assert($value === null || is_array($value));
+
+        return $value;
     }
 
     /**

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

@@ -151,4 +151,24 @@ class ArgumentsTest extends TestCase
         $this->assertSame('0', $args->getOption('zero'));
         $this->assertNull($args->getOption('undef'));
     }
+
+    public function testGetBooleanOption(): void
+    {
+        $options = [
+            'verbose' => true,
+        ];
+        $args = new Arguments([], $options, []);
+        $this->assertTrue($args->getBooleanOption('verbose'));
+        $this->assertNull($args->getBooleanOption('missing'));
+    }
+
+    public function testGetMultipleOption(): void
+    {
+        $options = [
+            'types' => ['one', 'two', 'three'],
+        ];
+        $args = new Arguments([], $options, []);
+        $this->assertSame(['one', 'two', 'three'], $args->getMultipleOption('types'));
+        $this->assertNull($args->getMultipleOption('missing'));
+    }
 }