-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: CLI: add promptByKey method #3912
Feature: CLI: add promptByKey method #3912
Conversation
This needs a user guide update, feel free to commit |
This seems to be very close in functionality to the existing CLI::prompt() as it already allows you to choose from a list. I don't see it as being significantly different from existing functionality, so closing. Feel free to comment if I'm wrong, though. |
The difference to |
@lonnieezell can I get your opinion on this with the updated explanation? |
I don't quite understand what you mean here. Can you give a visual example to this? |
Basically
$selectedKey = CLI::promptSelect('Select a type`, [
'foo' => 'Description of foo',
'bar' => 'Description of bar',
'foobar' => 'Description of foobar',
]);
// $selectedKey: 'bar' which results in the following output:
When trying the same with the $selectedKey = CLI::promptSelect('Select a type`, [
'foo' => 'Description of foo',
'bar' => 'Description of bar',
'foobar' => 'Description of foobar',
]);
// $selectedKey: 'Description of bar' which results in the following output (not really but for the sake of explaing the difference):
|
More or less I am getting the concept you want to achieve, but I want to confirm if this is different from what I am accomplishing via In my library's updater, I am using prompts to select something but with a description for each:
|
Yeah that is the same Idea, but
But going your way instead of mine but wrapped in a function seems like an option for me.
If you think that's the better solution, I can do that! Easy use case: Select a user permission level. You got an array of all levels & their translated descriptions and just want to select one. $user->setPermissionLevel( CLI::promptSelect($levels) ); This would be way more complicated not using |
Ok. But how do you make the selection? Via typing the keys or via the up/down arrow keys. IMO, if it is named promptSelect then I would be more or less expecting a CLI equivalent of an HTML dropdown list. If you will be just typing it too then I think thats why Lonnie thinks this is functionally the same with prompt. |
It is (right now) based on typing (like your own "implementation" of it). |
After pondering on this for a while, I've noticed that the code is structurally the same with
We can consider this then as an upgrade to prompt. |
Seems fine for me except for one thing: |
Hmm. I guess you're right. It seems a new method is the right way to go. Actually I want this feature in so that I can reduce some lines in my library. However, in order to promote DRY, we must ensure that the common code here with |
I'll do that! |
Works now & is DRY conform. CLI::promptSelect('Select one option:', ['foo', 'bar', 'foobar']);
//Select one option:
// 0 foo
// 1 bar
// 2 foobar
//
//[0, 1, 2]:
// -------------------------------------------------
CLI::promptSelect('Select one option:', [
'foo' => 'Description of foo',
'bar' => 'Description of bar',
'foobar' => 'Description of foobar'
]);
//Select one option:
// foo Description of foo
// bar Description of bar
// foobar Description of foobar
//
//[foo, bar, foobar]: |
I do write the user guide update when the code-basis is accepted 😀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the colors, I prefer green text for the options enclosed in square brackets.
Can you make the first param of promptSelect
accepts a string|array
? I am thinking that in addition to your proposed setup of the question on top of the choices then the selection list at the bottom, I would like to have some "preface" to the choices, then the question will be aligned similar to the original prompt
.
For example:
CLI::promptSelect(['These are your choices', 'Which would you like?'], [
'apple' => 'The red apple',
'orange' => 'The plump orange',
'banana' => 'The ripe banana'
], null);
// These are your choices
// [apple] The red apple
// [orange] The plump orange
// [banana] The ripe banana
//
// Which would you like? ['apple', 'orange', 'banana']
So basically what would happen is two options. If you passed a string text, this would be passed as the question itself printed above the choices and a PHP_EOL
is on the bottom with the selection. If I pass in an array, the first value is treated as the preface and the second value is treated as the question.
Please rebase again. I've merged your other PR which caused conflict with this. |
The idea with the double texts is great, thought about it multiple times how to implement a second text for the prompt. |
If using no second text, he should only supply a string text. And it will give that text to the preface and the question will still be using PHP_EOL, just like what you did. |
Changing default to green is not breaking, so I'm fine with the change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some minor CS fixes. Then lastly, the user guide addition.
I'd like to ask why change the padding to +6 from +2?
+2 for the added square brackets Working on the cs fixes & user guide in 2 weeks |
@element-code any update on this one? We just need a little more push to here before it's done. |
Timings change on the fly.. it's on my list; eta: after the next two weaks :( |
b759abb
to
8937977
Compare
Finally, found time for this
When committing and running git hooks it runs on the full code.
How is this supposed to be and how to work with that in the future? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good already. Thanks for taking some time to finish this. However, develop
is currently locked for styling and CS changes only. I'm afraid you need to rebase your branch against 4.2
For your concern on CS running on whole codebase is intended as after develop gets a release, all code should now be compliant. You can instead run it manually:
vendor/bin/php-cs-fixer fix -v file1 file2 file 3
Then, when committing just add the --no-verify
flag.
434fd07
to
a8a13b1
Compare
a8a13b1
to
11e4772
Compare
Thank you, @element-code |
Allows the developer to prompt for a key-based selection of a value.
This requires to have #3911 merged to complete the PHPStan Task.
Checklist: