1<?php 2/** 3 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) 4 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 5 * 6 * Licensed under The MIT License 7 * For full copyright and license information, please see the LICENSE.txt 8 * Redistributions of files must retain the above copyright notice. 9 * 10 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 11 * @link https://cakephp.org CakePHP Project 12 * @package Cake.Console.Command 13 * @since CakePHP v 2.5 14 * @license https://opensource.org/licenses/mit-license.php MIT License 15 */ 16 17App::uses('AppShell', 'Console/Command'); 18 19/** 20 * Provide command completion shells such as bash. 21 * 22 * @package Cake.Console.Command 23 */ 24class CompletionShell extends AppShell { 25 26/** 27 * Contains tasks to load and instantiate 28 * 29 * @var array 30 */ 31 public $tasks = array('Command'); 32 33/** 34 * Echo no header by overriding the startup method 35 * 36 * @return void 37 */ 38 public function startup() { 39 } 40 41/** 42 * Not called by the autocomplete shell - this is for curious users 43 * 44 * @return void 45 */ 46 public function main() { 47 return $this->out($this->getOptionParser()->help()); 48 } 49 50/** 51 * list commands 52 * 53 * @return void 54 */ 55 public function commands() { 56 $options = $this->Command->commands(); 57 return $this->_output($options); 58 } 59 60/** 61 * list options for the named command 62 * 63 * @return void 64 */ 65 public function options() { 66 $commandName = ''; 67 if (!empty($this->args[0])) { 68 $commandName = $this->args[0]; 69 } 70 $options = $this->Command->options($commandName); 71 72 return $this->_output($options); 73 } 74 75/** 76 * list subcommands for the named command 77 * 78 * @return void 79 */ 80 public function subCommands() { 81 if (!$this->args) { 82 return $this->_output(); 83 } 84 85 $options = $this->Command->subCommands($this->args[0]); 86 return $this->_output($options); 87 } 88 89/** 90 * Guess autocomplete from the whole argument string 91 * 92 * @return void 93 */ 94 public function fuzzy() { 95 return $this->_output(); 96 } 97 98/** 99 * Gets the option parser instance and configures it. 100 * 101 * @return ConsoleOptionParser 102 */ 103 public function getOptionParser() { 104 $parser = parent::getOptionParser(); 105 106 $parser->description( 107 __d('cake_console', 'Used by shells like bash to autocomplete command name, options and arguments') 108 )->addSubcommand('commands', array( 109 'help' => __d('cake_console', 'Output a list of available commands'), 110 'parser' => array( 111 'description' => __d('cake_console', 'List all availables'), 112 'arguments' => array( 113 ) 114 ) 115 ))->addSubcommand('subcommands', array( 116 'help' => __d('cake_console', 'Output a list of available subcommands'), 117 'parser' => array( 118 'description' => __d('cake_console', 'List subcommands for a command'), 119 'arguments' => array( 120 'command' => array( 121 'help' => __d('cake_console', 'The command name'), 122 'required' => true, 123 ) 124 ) 125 ) 126 ))->addSubcommand('options', array( 127 'help' => __d('cake_console', 'Output a list of available options'), 128 'parser' => array( 129 'description' => __d('cake_console', 'List options'), 130 'arguments' => array( 131 'command' => array( 132 'help' => __d('cake_console', 'The command name'), 133 'required' => false, 134 ) 135 ) 136 ) 137 ))->epilog( 138 __d('cake_console', 'This command is not intended to be called manually') 139 ); 140 141 return $parser; 142 } 143 144/** 145 * Emit results as a string, space delimited 146 * 147 * @param array $options The options to output 148 * @return void 149 */ 150 protected function _output($options = array()) { 151 if ($options) { 152 return $this->out(implode(' ', $options)); 153 } 154 } 155} 156