CompletionShell.php 3.5 KB

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