vivait / symfony-console-promptable-options
允许您通过问题指定使用 Symfony 控制台组件要询问的选项。
Requires
- php: >=5.5.9
- symfony/console: ^2.8|^3.0
- symfony/options-resolver: ^3.2
Requires (Dev)
- behat/behat: ^3.3
- phpunit/phpunit: ^5.7
- symfony/process: ^3.2
This package is not auto-updated.
Last update: 2022-06-11 08:39:49 UTC
README
兼容性 / 要求
- PHP 5.5.9 及以上
- symfony/console ^2.8|^3.0
安装
composer require vivait/symfony-console-promptable-options
用法
在您的命令的 configure()
方法中配置可提示选项。
您可以使用 $this->addPrompt(string $optionName, array $configuration = [])
与其他选项流畅地调用,以添加单个选项的提示。
或者,使用 $this->addPrompts(array $options = [])
与其他选项流畅地调用,通过提供选项名称和它们的期望配置的键值数组,同时添加多个选项的提示。这不会覆盖之前添加的提示,而是将它们添加到选项中。以下是添加多个提示的示例
protected function configure() { $this ->setName('promptable:test') ->addPrompts( [ 'name', // No configuration provided - uses defaults 'age' => ['type' => 'int', 'required' => true] ] ); }
配置完成后,使用 $this->getConsoleOptionInput(string $optionName)
访问选项。
下表显示了它在各种情况下的作用
标记为可提示的选项 | 不可提示的选项 | |
---|---|---|
通过 --optionName=value 提供 选项 |
将返回 --optionName=value 的值 |
将返回 --optionName=value 的值 |
未通过 --optionName=value 提供 选项 |
将通过交互式问题询问选项 | 将返回 null |
非交互式命令
如果您运行了一个具有可提示选项的命令,并且这些选项没有通过 --optionName=value
提供,则将抛出带有消息 "Cannot prompt for optionName, command is running in non-interactive mode"
的 \Exception
。
示例命令
<?php use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Vivait\PromptableOptions\Command\PromptableOptionsTrait; class PromptableCommand extends Command { use PromptableOptionsTrait; protected function configure() { $this ->setName('promptable:demo') ->addPrompt( 'name', ['description' => 'Your name', 'required' => true] ) // add a single thing to be prompted, which has a type or 'string' by default ->addPrompt('age', ['type' => 'int', 'description' => 'Your age', 'required' => true]) ->addPrompt('occupation', ['type' => 'string', 'description' => 'Your occupation', 'required' => true]) ->setDescription('To demonstrate the use of promptable input options'); // alternatively: $this ->setName('promptable:demo') ->addPrompts( [ 'name' => ['description' => 'Your name', 'required' => true], 'age' => ['type' => 'int', 'description' => 'Your age', 'required' => true], 'occupation' => ['description' => 'Your occupation', 'required' => true] ] ) // add multiple things to be prompted ->setDescription('To demonstrate the use of promptable input options'); } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->setUpPrompt($input, $output); $name = $this->getConsoleOptionInput('name'); $output->writeln(sprintf('Hello %s!', $name)); $age = $this->getConsoleOptionInput('age'); if ($age) { $output->writeln(sprintf('You are %s years old', $age)); } $occupation = $this->getConsoleOptionInput('occupation'); $output->writeln(sprintf('You are a %s!', $occupation)); } }
输出
当未指定任何选项时 - 可提示选项会被询问,不可提示选项仍然是可选的
当指定了一些选项时 - 可提示选项会被询问
当指定了所有选项时 - 不会提示输入
当指定了一些选项,并且使用 --no-interaction
运行命令时 - 当询问未指定的选项时,命令会出错
选项配置
每个选项都可用以下配置选项
配置值 | 描述 | 允许的值 |
---|---|---|
type | 预期值的类型(输入将被转换) | int 、integer 、float 、bool 、boolean 、string (作为字符串写入) |
description | 选项的描述/在询问选项时显示的文本 | 任何内容 |
必需 | 选项是否必需。如果设置为 true,则必须输入值,并且会持续提示直到提供值为止。如果设置为 false,将使用已设置的默认值,如果没有设置默认值,则返回 null 作为其输入。 |
true 或 false (作为布尔值) |
默认值 | 如果未提供(如果不必需),将使用此默认值 | 任何内容 |
贡献
这最初是我们内部使用于一些项目的一个项目,如果您认为有新的功能/想法可能有用,请随时提出建议,或提交 PR!
尽管这个项目很小,但我们非常重视开放性和包容性。为此,已采用以下行为准则。