1338 / smart-cli-select
cli tool
v1.2.1
2021-11-11 13:15 UTC
Requires
- php: >=7.4
- symfony/console: ^5.0
README
Symfony selection helper
Select array/objects while being able to add new options
How to call:
$smartCliSelect = new SmartCliSelect($inputInterface, $outputInterface, $questionHelper); $result = $smartSelector->smartSelect( "Question", ["preselected choices indexes"], ["choices"], false, // force preselection choices ["structure to create new options"] );
examples
Object
$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]; }
Object with a subset of options to be filled
$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]; }
Array
$hostResults = $smartSelector->smartSelect( 'Select hosts', array_keys($this->instances), $this->input->getOption('host'), false, // force preselection choices [ 'options' => ['host', 'username', 'pass', 'port'] ] );