ConsoleOptionParser.php 25 KB

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