0
我試圖編寫一個控制檯命令,其中需要幾個可選選項,但其中一個是必需的。如果沒有任何選項有值,我想打印命令的幫助描述。這個工作正常,只有一個例外 - 當我手動調用'help'命令時,'help'參數顯示在幫助屏幕上。在自定義Symfony命令調用的幫助下襬脫'command'參數
<?php
require __DIR__ .'/vendor/autoload.php';
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class TestCommand extends Symfony\Component\Console\Command\Command
{
public function configure()
{
$this->setName('test')
->setDescription('A test command.')
->setHelp('Help message.')
->addOption('one', null, InputOption::VALUE_OPTIONAL, 'Option 1', null)
->addOption('two', null, InputOption::VALUE_OPTIONAL, 'Option 2', null);
}
public function execute(InputInterface $in, OutputInterface $out)
{
$helpCommand = $this->getApplication()->get('help');
$helpCommand->run(new ArrayInput(['command_name' => $this->getName()]), $out);
return 0;
}
}
$app = new Symfony\Component\Console\Application;
$app->add(new TestCommand());
$app->run();
下面是help test
輸出:
[email protected]:~/sf2-console-test
$ ./run.php help test
Usage:
test [--one[="..."]] [--two[="..."]]
Options:
--one Option 1
--two Option 2
...
相比於剛剛help
輸出:
[email protected]:~/sf2-console-test
$ ./run.php test
Usage:
test [--one[="..."]] [--two[="..."]]
Arguments:
command The command to execute
Options:
--one Option 1
--two Option 2
...
有沒有擺脫 '命令' 參數的方法嗎?
'command'參數是Applicaiton :: getDefaultInputDefinition的一部分,但我在調用help命令之前試過$this->getApplication()->getDefinition()->setArguments([]);
,它似乎沒有什麼區別。
用symfony /控制檯V2.4.2以及DEV-主563254c
爲什麼你需要這樣的*「command-ception」*? – Touki
它在第一段中有解釋。 – Andreas