1338 / smart-cli-select
命令行工具
v1.2.1
2021-11-11 13:15 UTC
Requires
- php: >=7.4
- symfony/console: ^5.0
README
Symfony 选择助手
能够添加新选项的同时选择数组/对象
如何调用
$smartCliSelect = new SmartCliSelect($inputInterface, $outputInterface, $questionHelper); $result = $smartSelector->smartSelect( "Question", ["preselected choices indexes"], ["choices"], false, // force preselection choices ["structure to create new options"] );
示例
对象
$result = $smartSelector->smartSelect( "Select products", [$allProductsIndexedBySkus[0]], $allProductsIndexedBySkus, false, // force preselection choices ['type' => 'object'] ); $selectedProductIndices = $result['selected']; foreach ($result['new'] => $newProduct) { // do something to new objects, like for example: $this->entityManager->persist($newProduct); // } $allProductsIndexedBySkus += $result['new']; // add new products $selectedProducts = []; foreach ($selectedProductIndices as $index) { $selectedProducts[$index] = $allProductsIndexedBySkus[$index]; }
带有要填充的选项子集的对象
$result = $smartSelector->smartSelect( "Select products", [$allProductsIndexedBySkus[0]], $allProductsIndexedBySkus, false, // force preselection choices [ 'type' => 'object', 'options' => [ 'name', 'sku' ] ] ); $selectedProductIndices = $result['selected']; foreach ($result['new'] => $newProduct) { // do something to new objects, like for example: $this->entityManager->persist($newProduct); // } $allProductsIndexedBySkus += $result['new']; // add new products $selectedProducts = []; foreach ($selectedProductIndices as $index) { $selectedProducts[$index] = $allProductsIndexedBySkus[$index]; }
数组
$hostResults = $smartSelector->smartSelect( 'Select hosts', array_keys($this->instances), $this->input->getOption('host'), false, // force preselection choices [ 'options' => ['host', 'username', 'pass', 'port'] ] );