Browse Source

Start implementing the Arguments class.

Build out basic positional argument methods. I've skipped methods to
fetch positional arguments by name, as I don't think it makes sense now.
The whole point of positional arguments is that they are _positional_.

Refs #11137
Mark Story 8 years ago
parent
commit
365ae23e40
2 changed files with 149 additions and 0 deletions
  1. 84 0
      src/Console/Arguments.php
  2. 65 0
      tests/TestCase/Console/ArgumentsTest.php

+ 84 - 0
src/Console/Arguments.php

@@ -0,0 +1,84 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.6.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Console;
+
+/**
+ * Provides an interface for interacting with
+ * a command's options and arguments.
+ */
+class Arguments
+{
+    /**
+     * Positional arguments.
+     *
+     * @var array
+     */
+    protected $args;
+
+    /**
+     * Named options
+     *
+     * @var array
+     */
+    protected $options;
+
+    /**
+     * Constructor
+     *
+     * @param array $args Positional arguments
+     * @param array $options Named arguments
+     */
+    public function __construct(array $args, array $options)
+    {
+        $this->args = $args;
+        $this->options = $options;
+    }
+
+    /**
+     * Get all positional arguments.
+     *
+     * @return array
+     */
+    public function getArguments()
+    {
+        return $this->args;
+    }
+
+    /**
+     * Get positional arguments by index.
+     *
+     * @param int $index The argument index to access.
+     * @return string|null The argument value or null
+     */
+    public function getArgument($index)
+    {
+        if (isset($this->args[$index])) {
+            return $this->args[$index];
+        }
+
+        return null;
+    }
+
+    /**
+     * Check if a positional argument exists
+     *
+     * @param int $index The argument index to check.
+     * @return bool
+     */
+    public function hasArgument($index)
+    {
+        return isset($this->args[$index]);
+    }
+}

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

@@ -0,0 +1,65 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.6.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+
+namespace Cake\Test\TestSuite\Console;
+
+use Cake\Console\Arguments;
+use Cake\TestSuite\TestCase;
+
+/**
+ * Arguments test case.
+ */
+class ArgumentsTest extends TestCase
+{
+    /**
+     * Get all arguments
+     *
+     * @return void
+     */
+    public function testGetArguments()
+    {
+        $values = ['big', 'brown', 'bear'];
+        $args = new Arguments($values, []);
+        $this->assertSame($values, $args->getArguments());
+    }
+
+    /**
+     * Get arguments by index
+     *
+     * @return void
+     */
+    public function testGetArgument()
+    {
+        $values = ['big', 'brown', 'bear'];
+        $args = new Arguments($values, []);
+        $this->assertSame($values[0], $args->getArgument(0));
+        $this->assertSame($values[1], $args->getArgument(1));
+        $this->assertNull($args->getArgument(3));
+    }
+
+    /**
+     * check arguments by index
+     *
+     * @return void
+     */
+    public function testHasArgument()
+    {
+        $values = ['big', 'brown', 'bear'];
+        $args = new Arguments($values, []);
+        $this->assertTrue($args->hasArgument(0));
+        $this->assertTrue($args->hasArgument(1));
+        $this->assertFalse($args->hasArgument(3));
+    }
+}