CompletionShell.php 3.6 KB

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