在使用 Symfony Console 组件开发命令行应用时,经常会遇到参数类型不明确的问题。
InputInterface
getArgument()
getOption()
webignition/symfony-console-typed-input
在 symfony console 中,我们通常使用
InputInterface
InputInterface
getArgument()
getOption()
例如,我们需要获取一个名为
count
<pre class="brush:php;toolbar:false;">$count = $input->getOption('count');
if (!is_numeric($count)) {
    throw new \InvalidArgumentException('The count option must be an integer.');
}
$count = (int) $count;这段代码不仅冗长,而且容易出错。如果忘记进行类型转换,或者类型转换失败,可能会导致程序出现意想不到的行为。
webignition/symfony-console-typed-input
InputInterface
安装
webignition/symfony-console-typed-input
<pre class="brush:php;toolbar:false;">composer require webignition/symfony-console-typed-input
安装完成后,就可以在你的 Symfony Console 命令中使用它了:
<pre class="brush:php;toolbar:false;">use webignition\SymfonyConsole\TypedInput\TypedInput;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('my-command')
            ->addArgument('integer-argument', InputArgument::REQUIRED, 'An integer argument')
            ->addOption('integer-option', null, InputOption::VALUE_REQUIRED, 'An integer option')
            ->addOption('boolean-option', null, InputOption::VALUE_NONE, 'A boolean option');
    }
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $typedInput = new TypedInput($input);
        // Guaranteed to return an integer
        $integerArgument = $typedInput->getIntegerArgument('integer-argument');
        $integerOption = $typedInput->getIntegerOption('integer-option');
        // Guaranteed to return a boolean
        $booleanOption = $typedInput->getBooleanOption('boolean-option');
        $output->writeln('Integer Argument: ' . $integerArgument);
        $output->writeln('Integer Option: ' . $integerOption);
        $output->writeln('Boolean Option: ' . ($booleanOption ? 'true' : 'false'));
        return 0;
    }
}使用
TypedInput
getIntegerArgument()
getIntegerOption()
getBooleanArgument()
getBooleanOption()
webignition/symfony-console-typed-input
以上就是SymfonyConsole参数类型混乱?webignition/symfony-console-typed-input助你代码清晰!的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
 
                 
                                
                                 收藏
收藏
                                                                             
                                
                                 收藏
收藏
                                                                             
                                
                                 收藏
收藏
                                                                            Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号