ConsoleOptionParser.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console;
  16. use Cake\Console\Exception\ConsoleException;
  17. use Cake\Utility\Inflector;
  18. /**
  19. * Handles parsing the ARGV in the command line and provides support
  20. * for GetOpt compatible option definition. Provides a builder pattern implementation
  21. * for creating shell option parsers.
  22. *
  23. * ### Options
  24. *
  25. * Named arguments come in two forms, long and short. Long arguments are preceded
  26. * by two - and give a more verbose option name. i.e. `--version`. Short arguments are
  27. * preceded by one - and are only one character long. They usually match with a long option,
  28. * and provide a more terse alternative.
  29. *
  30. * ### Using Options
  31. *
  32. * Options can be defined with both long and short forms. By using `$parser->addOption()`
  33. * you can define new options. The name of the option is used as its long form, and you
  34. * can supply an additional short form, with the `short` option. Short options should
  35. * only be one letter long. Using more than one letter for a short option will raise an exception.
  36. *
  37. * Calling options can be done using syntax similar to most *nix command line tools. Long options
  38. * cane either include an `=` or leave it out.
  39. *
  40. * `cake myshell command --connection default --name=something`
  41. *
  42. * Short options can be defined signally or in groups.
  43. *
  44. * `cake myshell command -cn`
  45. *
  46. * Short options can be combined into groups as seen above. Each letter in a group
  47. * will be treated as a separate option. The previous example is equivalent to:
  48. *
  49. * `cake myshell command -c -n`
  50. *
  51. * Short options can also accept values:
  52. *
  53. * `cake myshell command -c default`
  54. *
  55. * ### Positional arguments
  56. *
  57. * If no positional arguments are defined, all of them will be parsed. If you define positional
  58. * arguments any arguments greater than those defined will cause exceptions. Additionally you can
  59. * declare arguments as optional, by setting the required param to false.
  60. *
  61. * `$parser->addArgument('model', ['required' => false]);`
  62. *
  63. * ### Providing Help text
  64. *
  65. * By providing help text for your positional arguments and named arguments, the ConsoleOptionParser
  66. * can generate a help display for you. You can view the help for shells by using the `--help` or `-h` switch.
  67. *
  68. */
  69. class ConsoleOptionParser {
  70. /**
  71. * Description text - displays before options when help is generated
  72. *
  73. * @see ConsoleOptionParser::description()
  74. * @var string
  75. */
  76. protected $_description = null;
  77. /**
  78. * Epilog text - displays after options when help is generated
  79. *
  80. * @see ConsoleOptionParser::epilog()
  81. * @var string
  82. */
  83. protected $_epilog = null;
  84. /**
  85. * Option definitions.
  86. *
  87. * @see ConsoleOptionParser::addOption()
  88. * @var array
  89. */
  90. protected $_options = [];
  91. /**
  92. * Map of short -> long options, generated when using addOption()
  93. *
  94. * @var string
  95. */
  96. protected $_shortOptions = [];
  97. /**
  98. * Positional argument definitions.
  99. *
  100. * @see ConsoleOptionParser::addArgument()
  101. * @var array
  102. */
  103. protected $_args = [];
  104. /**
  105. * Subcommands for this Shell.
  106. *
  107. * @see ConsoleOptionParser::addSubcommand()
  108. * @var array
  109. */
  110. protected $_subcommands = [];
  111. /**
  112. * Command name.
  113. *
  114. * @var string
  115. */
  116. protected $_command = '';
  117. /**
  118. * Construct an OptionParser so you can define its behavior
  119. *
  120. * @param string $command The command name this parser is for. The command name is used for generating help.
  121. * @param bool $defaultOptions Whether you want the verbose and quiet options set. Setting
  122. * this to false will prevent the addition of `--verbose` & `--quiet` options.
  123. */
  124. public function __construct($command = null, $defaultOptions = true) {
  125. $this->command($command);
  126. $this->addOption('help', [
  127. 'short' => 'h',
  128. 'help' => 'Display this help.',
  129. 'boolean' => true
  130. ]);
  131. if ($defaultOptions) {
  132. $this->addOption('verbose', [
  133. 'short' => 'v',
  134. 'help' => 'Enable verbose output.',
  135. 'boolean' => true
  136. ])->addOption('quiet', [
  137. 'short' => 'q',
  138. 'help' => 'Enable quiet output.',
  139. 'boolean' => true
  140. ]);
  141. }
  142. }
  143. /**
  144. * Static factory method for creating new OptionParsers so you can chain methods off of them.
  145. *
  146. * @param string $command The command name this parser is for. The command name is used for generating help.
  147. * @param bool $defaultOptions Whether you want the verbose and quiet options set.
  148. * @return ConsoleOptionParser
  149. */
  150. public static function create($command, $defaultOptions = true) {
  151. return new ConsoleOptionParser($command, $defaultOptions);
  152. }
  153. /**
  154. * Build a parser from an array. Uses an array like
  155. *
  156. * {{{
  157. * $spec = [
  158. * 'description' => 'text',
  159. * 'epilog' => 'text',
  160. * 'arguments' => [
  161. * // list of arguments compatible with addArguments.
  162. * ],
  163. * 'options' => [
  164. * // list of options compatible with addOptions
  165. * ],
  166. * 'subcommands' => [
  167. * // list of subcommands to add.
  168. * ]
  169. * ];
  170. * }}}
  171. *
  172. * @param array $spec The spec to build the OptionParser with.
  173. * @param bool $defaultOptions Whether you want the verbose and quiet options set.
  174. * @return ConsoleOptionParser
  175. */
  176. public static function buildFromArray($spec, $defaultOptions = true) {
  177. $parser = new ConsoleOptionParser($spec['command'], $defaultOptions);
  178. if (!empty($spec['arguments'])) {
  179. $parser->addArguments($spec['arguments']);
  180. }
  181. if (!empty($spec['options'])) {
  182. $parser->addOptions($spec['options']);
  183. }
  184. if (!empty($spec['subcommands'])) {
  185. $parser->addSubcommands($spec['subcommands']);
  186. }
  187. if (!empty($spec['description'])) {
  188. $parser->description($spec['description']);
  189. }
  190. if (!empty($spec['epilog'])) {
  191. $parser->epilog($spec['epilog']);
  192. }
  193. return $parser;
  194. }
  195. /**
  196. * Returns an array representation of this parser.
  197. *
  198. * @return array
  199. */
  200. public function toArray() {
  201. $result = [
  202. 'command' => $this->_command,
  203. 'arguments' => $this->_args,
  204. 'options' => $this->_options,
  205. 'subcommands' => $this->_subcommands,
  206. 'description' => $this->_description,
  207. 'epilog' => $this->_epilog
  208. ];
  209. return $result;
  210. }
  211. /**
  212. * Get or set the command name for shell/task.
  213. *
  214. * @param array|\Cake\Console\ConsoleOptionParser $parser ConsoleOptionParser or spec to merge with.
  215. * @return $this
  216. */
  217. public function merge($spec) {
  218. if ($spec instanceof ConsoleOptionParser) {
  219. $spec = $spec->toArray();
  220. }
  221. if (!empty($spec['arguments'])) {
  222. $this->addArguments($spec['arguments']);
  223. }
  224. if (!empty($spec['options'])) {
  225. $this->addOptions($spec['options']);
  226. }
  227. if (!empty($spec['subcommands'])) {
  228. $this->addSubcommands($spec['subcommands']);
  229. }
  230. if (!empty($spec['description'])) {
  231. $this->description($spec['description']);
  232. }
  233. if (!empty($spec['epilog'])) {
  234. $this->epilog($spec['epilog']);
  235. }
  236. return $this;
  237. }
  238. /**
  239. * Gets or sets the command name for shell/task.
  240. *
  241. * @param string $text The text to set, or null if you want to read
  242. * @return string|$this If reading, the value of the command. If setting $this will be returned.
  243. */
  244. public function command($text = null) {
  245. if ($text !== null) {
  246. $this->_command = Inflector::underscore($text);
  247. return $this;
  248. }
  249. return $this->_command;
  250. }
  251. /**
  252. * Get or set the description text for shell/task.
  253. *
  254. * @param string|array $text The text to set, or null if you want to read. If an array the
  255. * text will be imploded with "\n"
  256. * @return string|$this If reading, the value of the description. If setting $this will be returned.
  257. */
  258. public function description($text = null) {
  259. if ($text !== null) {
  260. if (is_array($text)) {
  261. $text = implode("\n", $text);
  262. }
  263. $this->_description = $text;
  264. return $this;
  265. }
  266. return $this->_description;
  267. }
  268. /**
  269. * Get or set an epilog to the parser. The epilog is added to the end of
  270. * the options and arguments listing when help is generated.
  271. *
  272. * @param string|array $text Text when setting or null when reading. If an array the text will be imploded with "\n"
  273. * @return string|$this If reading, the value of the epilog. If setting $this will be returned.
  274. */
  275. public function epilog($text = null) {
  276. if ($text !== null) {
  277. if (is_array($text)) {
  278. $text = implode("\n", $text);
  279. }
  280. $this->_epilog = $text;
  281. return $this;
  282. }
  283. return $this->_epilog;
  284. }
  285. /**
  286. * Add an option to the option parser. Options allow you to define optional or required
  287. * parameters for your console application. Options are defined by the parameters they use.
  288. *
  289. * ### Options
  290. *
  291. * - `short` - The single letter variant for this option, leave undefined for none.
  292. * - `help` - Help text for this option. Used when generating help for the option.
  293. * - `default` - The default value for this option. Defaults are added into the parsed params when the
  294. * attached option is not provided or has no value. Using default and boolean together will not work.
  295. * are added into the parsed parameters when the option is undefined. Defaults to null.
  296. * - `boolean` - The option uses no value, its just a boolean switch. Defaults to false.
  297. * If an option is defined as boolean, it will always be added to the parsed params. If no present
  298. * it will be false, if present it will be true.
  299. * - `choices` A list of valid choices for this option. If left empty all values are valid..
  300. * An exception will be raised when parse() encounters an invalid value.
  301. *
  302. * @param ConsoleInputOption|string $name The long name you want to the value to be parsed out as when options are parsed.
  303. * Will also accept an instance of ConsoleInputOption
  304. * @param array $options An array of parameters that define the behavior of the option
  305. * @return $this
  306. */
  307. public function addOption($name, $options = []) {
  308. if ($name instanceof ConsoleInputOption) {
  309. $option = $name;
  310. $name = $option->name();
  311. } else {
  312. $defaults = [
  313. 'name' => $name,
  314. 'short' => null,
  315. 'help' => '',
  316. 'default' => null,
  317. 'boolean' => false,
  318. 'choices' => []
  319. ];
  320. $options += $defaults;
  321. $option = new ConsoleInputOption($options);
  322. }
  323. $this->_options[$name] = $option;
  324. if ($option->short() !== null) {
  325. $this->_shortOptions[$option->short()] = $name;
  326. }
  327. return $this;
  328. }
  329. /**
  330. * Remove an option from the option parser.
  331. *
  332. * @param string $name The option name to remove.
  333. * @return ConsoleOptionParser this
  334. */
  335. public function removeOption($name) {
  336. unset($this->_options[$name]);
  337. return $this;
  338. }
  339. /**
  340. * Add a positional argument to the option parser.
  341. *
  342. * ### Params
  343. *
  344. * - `help` The help text to display for this argument.
  345. * - `required` Whether this parameter is required.
  346. * - `index` The index for the arg, if left undefined the argument will be put
  347. * onto the end of the arguments. If you define the same index twice the first
  348. * option will be overwritten.
  349. * - `choices` A list of valid choices for this argument. If left empty all values are valid..
  350. * An exception will be raised when parse() encounters an invalid value.
  351. *
  352. * @param ConsoleInputArgument|string $name The name of the argument. Will also accept an instance of ConsoleInputArgument
  353. * @param array $params Parameters for the argument, see above.
  354. * @return $this
  355. */
  356. public function addArgument($name, $params = []) {
  357. if ($name instanceof ConsoleInputArgument) {
  358. $arg = $name;
  359. $index = count($this->_args);
  360. } else {
  361. $defaults = [
  362. 'name' => $name,
  363. 'help' => '',
  364. 'index' => count($this->_args),
  365. 'required' => false,
  366. 'choices' => []
  367. ];
  368. $options = $params + $defaults;
  369. $index = $options['index'];
  370. unset($options['index']);
  371. $arg = new ConsoleInputArgument($options);
  372. }
  373. $this->_args[$index] = $arg;
  374. ksort($this->_args);
  375. return $this;
  376. }
  377. /**
  378. * Add multiple arguments at once. Take an array of argument definitions.
  379. * The keys are used as the argument names, and the values as params for the argument.
  380. *
  381. * @param array $args Array of arguments to add.
  382. * @see ConsoleOptionParser::addArgument()
  383. * @return $this
  384. */
  385. public function addArguments(array $args) {
  386. foreach ($args as $name => $params) {
  387. if ($params instanceof ConsoleInputArgument) {
  388. $name = $params;
  389. $params = [];
  390. }
  391. $this->addArgument($name, $params);
  392. }
  393. return $this;
  394. }
  395. /**
  396. * Add multiple options at once. Takes an array of option definitions.
  397. * The keys are used as option names, and the values as params for the option.
  398. *
  399. * @param array $options Array of options to add.
  400. * @see ConsoleOptionParser::addOption()
  401. * @return $this
  402. */
  403. public function addOptions(array $options) {
  404. foreach ($options as $name => $params) {
  405. if ($params instanceof ConsoleInputOption) {
  406. $name = $params;
  407. $params = [];
  408. }
  409. $this->addOption($name, $params);
  410. }
  411. return $this;
  412. }
  413. /**
  414. * Append a subcommand to the subcommand list.
  415. * Subcommands are usually methods on your Shell, but can also be used to document Tasks.
  416. *
  417. * ### Options
  418. *
  419. * - `help` - Help text for the subcommand.
  420. * - `parser` - A ConsoleOptionParser for the subcommand. This allows you to create method
  421. * specific option parsers. When help is generated for a subcommand, if a parser is present
  422. * it will be used.
  423. *
  424. * @param ConsoleInputSubcommand|string $name Name of the subcommand. Will also accept an instance of ConsoleInputSubcommand
  425. * @param array $options Array of params, see above.
  426. * @return $this
  427. */
  428. public function addSubcommand($name, $options = []) {
  429. if ($name instanceof ConsoleInputSubcommand) {
  430. $command = $name;
  431. $name = $command->name();
  432. } else {
  433. $defaults = [
  434. 'name' => $name,
  435. 'help' => '',
  436. 'parser' => null
  437. ];
  438. $options += $defaults;
  439. $command = new ConsoleInputSubcommand($options);
  440. }
  441. $this->_subcommands[$name] = $command;
  442. return $this;
  443. }
  444. /**
  445. * Remove a subcommand from the option parser.
  446. *
  447. * @param string $name The subcommand name to remove.
  448. * @return $this
  449. */
  450. public function removeSubcommand($name) {
  451. unset($this->_subcommands[$name]);
  452. return $this;
  453. }
  454. /**
  455. * Add multiple subcommands at once.
  456. *
  457. * @param array $commands Array of subcommands.
  458. * @return $this
  459. */
  460. public function addSubcommands(array $commands) {
  461. foreach ($commands as $name => $params) {
  462. if ($params instanceof ConsoleInputSubcommand) {
  463. $name = $params;
  464. $params = [];
  465. }
  466. $this->addSubcommand($name, $params);
  467. }
  468. return $this;
  469. }
  470. /**
  471. * Gets the arguments defined in the parser.
  472. *
  473. * @return array Array of argument descriptions
  474. */
  475. public function arguments() {
  476. return $this->_args;
  477. }
  478. /**
  479. * Get the defined options in the parser.
  480. *
  481. * @return array
  482. */
  483. public function options() {
  484. return $this->_options;
  485. }
  486. /**
  487. * Get the array of defined subcommands
  488. *
  489. * @return array
  490. */
  491. public function subcommands() {
  492. return $this->_subcommands;
  493. }
  494. /**
  495. * Parse the argv array into a set of params and args. If $command is not null
  496. * and $command is equal to a subcommand that has a parser, that parser will be used
  497. * to parse the $argv
  498. *
  499. * @param array $argv Array of args (argv) to parse.
  500. * @return array [$params, $args]
  501. * @throws \Cake\Console\Exception\ConsoleException When an invalid parameter is encountered.
  502. */
  503. public function parse($argv) {
  504. $command = isset($argv[0]) ? $argv[0] : null;
  505. if (isset($this->_subcommands[$command])) {
  506. array_shift($argv);
  507. }
  508. if (isset($this->_subcommands[$command]) && $this->_subcommands[$command]->parser()) {
  509. return $this->_subcommands[$command]->parser()->parse($argv);
  510. }
  511. $params = $args = [];
  512. $this->_tokens = $argv;
  513. while (($token = array_shift($this->_tokens)) !== null) {
  514. if (isset($this->_subcommands[$token])) {
  515. continue;
  516. } elseif (substr($token, 0, 2) === '--') {
  517. $params = $this->_parseLongOption($token, $params);
  518. } elseif (substr($token, 0, 1) === '-') {
  519. $params = $this->_parseShortOption($token, $params);
  520. } else {
  521. $args = $this->_parseArg($token, $args);
  522. }
  523. }
  524. foreach ($this->_args as $i => $arg) {
  525. if ($arg->isRequired() && !isset($args[$i]) && empty($params['help'])) {
  526. throw new ConsoleException(
  527. sprintf('Missing required arguments. %s is required.', $arg->name())
  528. );
  529. }
  530. }
  531. foreach ($this->_options as $option) {
  532. $name = $option->name();
  533. $isBoolean = $option->isBoolean();
  534. $default = $option->defaultValue();
  535. if ($default !== null && !isset($params[$name]) && !$isBoolean) {
  536. $params[$name] = $default;
  537. }
  538. if ($isBoolean && !isset($params[$name])) {
  539. $params[$name] = false;
  540. }
  541. }
  542. return [$params, $args];
  543. }
  544. /**
  545. * Gets formatted help for this parser object.
  546. * Generates help text based on the description, options, arguments, subcommands and epilog
  547. * in the parser.
  548. *
  549. * @param string $subcommand If present and a valid subcommand that has a linked parser.
  550. * That subcommands help will be shown instead.
  551. * @param string $format Define the output format, can be text or xml
  552. * @param int $width The width to format user content to. Defaults to 72
  553. * @return string Generated help.
  554. */
  555. public function help($subcommand = null, $format = 'text', $width = 72) {
  556. if (
  557. isset($this->_subcommands[$subcommand]) &&
  558. $this->_subcommands[$subcommand]->parser() instanceof self
  559. ) {
  560. $subparser = $this->_subcommands[$subcommand]->parser();
  561. $subparser->command($this->command() . ' ' . $subparser->command());
  562. return $subparser->help(null, $format, $width);
  563. }
  564. $formatter = new HelpFormatter($this);
  565. if ($format === 'text') {
  566. return $formatter->text($width);
  567. }
  568. if ($format === 'xml') {
  569. return $formatter->xml();
  570. }
  571. }
  572. /**
  573. * Parse the value for a long option out of $this->_tokens. Will handle
  574. * options with an `=` in them.
  575. *
  576. * @param string $option The option to parse.
  577. * @param array $params The params to append the parsed value into
  578. * @return array Params with $option added in.
  579. */
  580. protected function _parseLongOption($option, $params) {
  581. $name = substr($option, 2);
  582. if (strpos($name, '=') !== false) {
  583. list($name, $value) = explode('=', $name, 2);
  584. array_unshift($this->_tokens, $value);
  585. }
  586. return $this->_parseOption($name, $params);
  587. }
  588. /**
  589. * Parse the value for a short option out of $this->_tokens
  590. * If the $option is a combination of multiple shortcuts like -otf
  591. * they will be shifted onto the token stack and parsed individually.
  592. *
  593. * @param string $option The option to parse.
  594. * @param array $params The params to append the parsed value into
  595. * @return array Params with $option added in.
  596. * @throws \Cake\Console\Exception\ConsoleException When unknown short options are encountered.
  597. */
  598. protected function _parseShortOption($option, $params) {
  599. $key = substr($option, 1);
  600. if (strlen($key) > 1) {
  601. $flags = str_split($key);
  602. $key = $flags[0];
  603. for ($i = 1, $len = count($flags); $i < $len; $i++) {
  604. array_unshift($this->_tokens, '-' . $flags[$i]);
  605. }
  606. }
  607. if (!isset($this->_shortOptions[$key])) {
  608. throw new ConsoleException(sprintf('Unknown short option `%s`', $key));
  609. }
  610. $name = $this->_shortOptions[$key];
  611. return $this->_parseOption($name, $params);
  612. }
  613. /**
  614. * Parse an option by its name index.
  615. *
  616. * @param string $name The name to parse.
  617. * @param array $params The params to append the parsed value into
  618. * @return array Params with $option added in.
  619. * @throws \Cake\Console\Exception\ConsoleException
  620. */
  621. protected function _parseOption($name, $params) {
  622. if (!isset($this->_options[$name])) {
  623. throw new ConsoleException(sprintf('Unknown option `%s`', $name));
  624. }
  625. $option = $this->_options[$name];
  626. $isBoolean = $option->isBoolean();
  627. $nextValue = $this->_nextToken();
  628. $emptyNextValue = (empty($nextValue) && $nextValue !== '0');
  629. if (!$isBoolean && !$emptyNextValue && !$this->_optionExists($nextValue)) {
  630. array_shift($this->_tokens);
  631. $value = $nextValue;
  632. } elseif ($isBoolean) {
  633. $value = true;
  634. } else {
  635. $value = $option->defaultValue();
  636. }
  637. if ($option->validChoice($value)) {
  638. $params[$name] = $value;
  639. return $params;
  640. }
  641. return array();
  642. }
  643. /**
  644. * Check to see if $name has an option (short/long) defined for it.
  645. *
  646. * @param string $name The name of the option.
  647. * @return bool
  648. */
  649. protected function _optionExists($name) {
  650. if (substr($name, 0, 2) === '--') {
  651. return isset($this->_options[substr($name, 2)]);
  652. }
  653. if ($name{0} === '-' && $name{1} !== '-') {
  654. return isset($this->_shortOptions[$name{1}]);
  655. }
  656. return false;
  657. }
  658. /**
  659. * Parse an argument, and ensure that the argument doesn't exceed the number of arguments
  660. * and that the argument is a valid choice.
  661. *
  662. * @param string $argument The argument to append
  663. * @param array $args The array of parsed args to append to.
  664. * @return array Args
  665. * @throws \Cake\Console\Exception\ConsoleException
  666. */
  667. protected function _parseArg($argument, $args) {
  668. if (empty($this->_args)) {
  669. $args[] = $argument;
  670. return $args;
  671. }
  672. $next = count($args);
  673. if (!isset($this->_args[$next])) {
  674. throw new ConsoleException('Too many arguments.');
  675. }
  676. if ($this->_args[$next]->validChoice($argument)) {
  677. $args[] = $argument;
  678. return $args;
  679. }
  680. }
  681. /**
  682. * Find the next token in the argv set.
  683. *
  684. * @return string next token or ''
  685. */
  686. protected function _nextToken() {
  687. return isset($this->_tokens[0]) ? $this->_tokens[0] : '';
  688. }
  689. }