cakephp 4 consoleoptionparser用法
在我的CakePHP 4.0项目中,我正在努力实现我认为是一个相当微不足道的目标:我希望拥有一个“基础”控制台命令,并具有一些基本的设置以及其他扩展其的类。
具体来说,我想在我的基类中定义一个[ consoleoptionparser
] [1],因为所有其他命令类都应访问相同的选项:
<?php
namespace Import\Shell;
use Cake\Command\Command;
use Cake\Console\ConsoleOptionParser;
class BaseImportCommand extends Command
{
public function __construct()
{
parent::__construct();
// setup some stuff related to my project here
}
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
// Get an empty parser from the framework.
$parser = parent::getOptionParser();
// Define your options and arguments.
$parser->addOptions(
[
'country' => [
'short' => 'c',
'help' => 'The country for which to execute the operation.',
'required' => false,
],
'author' => [
'short' => 'a',
'help' => 'The ID of the author for which to execute the operation.',
'required' => false,
],
'product' => [
'short' => 'p',
'help' => 'The ID of the product for which to execute the operation.',
'required' => false,
],
]
);
// Return the completed parser
return $parser;
}
}
<?php
namespace Import\Shell;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
class ProcessProductImagesCommand extends BaseImportCommand
{
public function __construct()
{
parent::__construct();
// setup some more stuff here
}
/**
* execute() method.
*
* @return bool|int|null Success or error code.
*/
public function execute(Arguments $args, ConsoleIo $io)
{
$country = $args->getOption('country');
$productId = $args->getOption('product');
// do my logic here
}
}
问题是,当我
bin/cake processProductImages -c CH
在Shell I中 运行时,得到这个错误:
Error: Unknown short option `c`.
为什么?我没有重新定义 buildoptionparser
方法 processProductimagesCommand
类,因此我假设 consoleoptionparser
配置是从 basecommand
basecommand
/代码>类。
为了解决该问题,我尝试将此方法添加到 ProcessProductimagesCommand
类:
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
return parent::buildOptionParser($parser);
}
但是在这种情况下,当我
bin/cake processProductImages -c CH
在shell中运行时会发生什么,然后才会遇到此错误:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 4096 bytes) in /var/www/repo/public/vendor/cakephp/cakephp/src/Console/ConsoleOptionParser.php on line 430
我找到了唯一的实际方法要拥有我需要的选择,在我需要的课堂中,通过复制整个 consoleoptionparser
在子类中重复的初始化,从 baseImportCommand
类,但显然我不喜欢此解决方案,因为它会导致无用的代码重复。
[1]: https://book.cake.cakephp.org/ 4/en/console-commands/option-parsers.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过惯例 命令< /strong> 应该生活在
命令
名称空间中,而不是shell
namespace。您无法调用
parent :: buildoptionparser()
没有任何参数。addargument()
的第一个参数是\ cake \ console \ consoleinputargument
,而不是数组。 > 可以使用addarguments()
方法一次添加(位置值)和 选项 是两种不同的东西,
-c
是需要使用addoption()
。配置的选项。
如果您在选项解析阶段耗尽了130多MB的内存,那么您可能会创建一个无限的循环。
By convention commands are supposed to live in the
Command
namespace, not theShell
namespace.You cannot call
parent::buildOptionParser()
without any arguments.The first argument of
addArgument()
is meant to be a string, or an instance of\Cake\Console\ConsoleInputArgument
, not an array. Multiple arguments can be added at once using theaddArguments()
method (note the trailings
).Arguments (positional values) and options are two different things,
-c
is an option that needs to be configured usingaddOption()
.If you're exhausting 130+ MB of memory in the options parsing stage, then you possibly created an infinite loop.