Browse Source

Improve the options completion by allowing to get options for subcommands

Yves P 10 years ago
parent
commit
bd505410c3

+ 9 - 2
src/Shell/CompletionShell.php

@@ -66,11 +66,14 @@ class CompletionShell extends Shell
      */
     public function options()
     {
-        $commandName = '';
+        $commandName = $subCommandName = '';
         if (!empty($this->args[0])) {
             $commandName = $this->args[0];
         }
-        $options = $this->Command->options($commandName);
+        if (!empty($this->args[1])) {
+            $subCommandName = $this->args[1];
+        }
+        $options = $this->Command->options($commandName, $subCommandName);
 
         return $this->_output($options);
     }
@@ -135,6 +138,10 @@ class CompletionShell extends Shell
                     'command' => [
                         'help' => 'The command name',
                         'required' => false,
+                    ],
+                    'subcommand' => [
+                        'help' => 'The subcommand name',
+                        'required' => false,
                     ]
                 ]
             ]

+ 21 - 9
src/Shell/Task/CommandTask.php

@@ -174,8 +174,8 @@ class CommandTask extends Shell
     /**
      * Get Shell instance for the given command
      *
-     * @param mixed $commandName The command you want.
-     * @return mixed
+     * @param string $commandName The command you want.
+     * @return \Cake\Console\Shell|bool Shell instance if the command can be found, false otherwise.
      */
     public function getShell($commandName)
     {
@@ -219,18 +219,30 @@ class CommandTask extends Shell
     }
 
     /**
-     * Get Shell instance for the given command
+     * Get options list for the given command or subcommand
      *
-     * @param mixed $commandName The command to get options for.
-     * @return array
+     * @param string $commandName The command to get options for.
+     * @param string $subCommandName The subcommand to get options for. Can be empty to get options for the command.
+     * If this parameter is used, the subcommand must be a valid subcommand of the command passed
+     * @return array Options list for the given command or subcommand
      */
-    public function options($commandName)
+    public function options($commandName, $subCommandName = '')
     {
         $Shell = $this->getShell($commandName);
+
         if (!$Shell) {
-            $parser = new ConsoleOptionParser();
-        } else {
-            $parser = $Shell->getOptionParser();
+            return [];
+        }
+
+        $parser = $Shell->getOptionParser();
+
+        if (!empty($subCommandName)) {
+            $subCommandName = Inflector::camelize($subCommandName);
+            if ($Shell->hasTask($subCommandName)) {
+                $parser = $Shell->{$subCommandName}->getOptionParser();
+            } else {
+                return [];
+            }
         }
 
         $options = [];

+ 21 - 8
tests/TestCase/Shell/CompletionShellTest.php

@@ -128,7 +128,7 @@ class CompletionShellTest extends TestCase
     }
 
     /**
-     * test that options without argument returns the default options
+     * test that options without argument returns nothing
      *
      * @return void
      */
@@ -137,12 +137,12 @@ class CompletionShellTest extends TestCase
         $this->Shell->runCommand(['options']);
         $output = $this->out->output;
 
-        $expected = "--help -h --verbose -v --quiet -q\n";
+        $expected = "";
         $this->assertTextEquals($expected, $output);
     }
 
     /**
-     * test that options with a nonexisting command returns the default options
+     * test that options with a nonexisting command returns nothing
      *
      * @return void
      */
@@ -150,13 +150,12 @@ class CompletionShellTest extends TestCase
     {
         $this->Shell->runCommand(['options', 'foo']);
         $output = $this->out->output;
-
-        $expected = "--help -h --verbose -v --quiet -q\n";
+        $expected = "";
         $this->assertTextEquals($expected, $output);
     }
 
     /**
-     * test that options with a existing command returns the proper options
+     * test that options with an existing command returns the proper options
      *
      * @return void
      */
@@ -170,6 +169,20 @@ class CompletionShellTest extends TestCase
     }
 
     /**
+     * test that options with an existing command / subcommand pair returns the proper options
+     *
+     * @return void
+     */
+    public function testOptionsTask()
+    {
+        $this->Shell->runCommand(['options', 'sample', 'sample']);
+        $output = $this->out->output;
+
+        $expected = "--help -h --verbose -v --quiet -q --sample -s\n";
+        $this->assertTextEquals($expected, $output);
+    }
+
+    /**
      * test that subCommands with a existing CORE command returns the proper sub commands
      *
      * @return void
@@ -193,7 +206,7 @@ class CompletionShellTest extends TestCase
         $this->Shell->runCommand(['subcommands', 'app.sample']);
         $output = $this->out->output;
 
-        $expected = "derp\n";
+        $expected = "derp sample\n";
         $this->assertTextEquals($expected, $output);
     }
 
@@ -251,7 +264,7 @@ class CompletionShellTest extends TestCase
         $this->Shell->runCommand(['subcommands', 'sample']);
         $output = $this->out->output;
 
-        $expected = "derp\n";
+        $expected = "derp sample\n";
         $this->assertTextEquals($expected, $output);
     }
 

+ 2 - 0
tests/test_app/TestApp/Shell/SampleShell.php

@@ -26,6 +26,8 @@ use Cake\Console\Shell;
 class SampleShell extends Shell
 {
 
+    public $tasks = ['Sample'];
+
     /**
      * main method
      *

+ 33 - 0
tests/test_app/TestApp/Shell/Task/SampleTask.php

@@ -0,0 +1,33 @@
+<?php
+/**
+ * SampleTask file
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         1.2.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\Shell\Task;
+
+use Cake\Console\Shell;
+
+class SampleTask extends Shell
+{
+
+    public function getOptionParser()
+    {
+        $parser = parent::getOptionParser();
+        $parser->addOption('sample', [
+            'short' => 's',
+            'help' => 'This is a sample option for the sample task.',
+        ]);
+        return $parser;
+    }
+}

+ 0 - 0
tests/test_app/TestApp/Shell/Task/empty