Add new method promptByMultipleKeys() in CLI class#6302
Add new method promptByMultipleKeys() in CLI class#6302kenjis merged 14 commits intocodeigniter4:4.3from
promptByMultipleKeys() in CLI class#6302Conversation
- make a test for the method - write the method in user guide
- fix title urderline in user guide
promptByKey() in CLI class
promptByKey() in CLI classpromptByMultipleKey() in CLI class
promptByMultipleKey() in CLI classpromptByMultipleKeys() in CLI class
paulbalandan
left a comment
There was a problem hiding this comment.
If this is the same with promptByKey() except for support for multiple keys. Please extract the common code in them instead of duplicating. This will make maintenance easier.
My initial comments below:
system/CLI/CLI.php
Outdated
| if (is_string($text)) { | ||
| $text = [$text]; | ||
| } elseif (! is_array($text)) { | ||
| throw new InvalidArgumentException('$text can only be string'); | ||
| } |
There was a problem hiding this comment.
This part is confusing.
- The PHPDoc says
$textis astring. - The method parameter says it is untyped.
- The conditional says
$textmust be astring|array - The throwable says
$textcan only be astring
system/CLI/CLI.php
Outdated
| throw new InvalidArgumentException('$text can only be string'); | ||
| } | ||
|
|
||
| if (is_array($options) && $options) { |
There was a problem hiding this comment.
is_array check is always true because of the type declaration above
system/CLI/CLI.php
Outdated
| } else { | ||
| throw new InvalidArgumentException('$options can only be array'); | ||
| } |
There was a problem hiding this comment.
Based on the previous if, if $options is an array but empty, the throwable will say $options can only be an array which is misleading.
system/CLI/CLI.php
Outdated
| CLI::write(CLI::color($name, 'green') . CLI::wrap($description, 125, $keyMaxLength + 4)); | ||
| } | ||
| static::fwrite(STDOUT, (trim((string) ((int) $text)) ? ' ' : '') . $extraOutput . ': '); | ||
| $input = trim(static::input()) ?: $default; |
There was a problem hiding this comment.
This will hang for 6 hours in the GH actions because the unit test is waiting for input. If you can in a way extract the non-testable part(s) in another method so that it can be mocked in tests.
There was a problem hiding this comment.
Now we cannot write test for it. See #6076
It is a problem.
There was a problem hiding this comment.
Hmm. This is the problem with using static methods. It is not mockable by PHPUnit.
There was a problem hiding this comment.
@paulbalandan It seems difficult to ask the contributor to add test code.
Do we accept this PR without test code?
There was a problem hiding this comment.
Ah yes. What I did before in promptByKey PR is to check locally the changes then suggest changes, if any, since prompt is not testable.
- delete unit test for promptByMultipleKeys because codeigniter4#6076 - change method name in docs and changelog
|
Sorry, this branch has conflicts. Can you rebase and solve the conflicts? And now we can write test for CLI::input(). See #6335 |
okay. i will write the test |
…to add-method-promptByMultipleKey-in-CLI
687e80f to
d886c9d
Compare
|
$ php public/index.php
Select your hobbies:
[0] Playing game
[1] Sleep
[2] Badminton
[0, 1, 2]: x
The temp field must be one of: 0, 1, 2.
[0, 1, 2]:
$ php public/index.php
Select your hobbies:
[0] Playing game
[1] Sleep
[2] Badminton
[0, 1, 2]
x
Please select correctly
Select your hobbies:
[0] Playing game
[1] Sleep
[2] Badminton
[0, 1, 2]
|
|
The output format is a bit different from
For example: $ php public/index.php
Select your hobbies:
[0] Playing game
[1] Sleep
[2] Badminton
You can specify multiple values separated by commas.
[0, 1, 2]: x
Please select correctly.
You can specify multiple values separated by commas.
[0, 1, 2]: |
|
I think key value pair is okay.
End users don't know whether |
In But in Is it fine? or should I replace the return in |
|
My opinion is the following: $ php public/index.php
Select your hobbies:
[0] Playing game
[1] Sleep
[2] Badminton
You can specify multiple values separated by commas.
[0, 1, 2]: x
Please select correctly.
You can specify multiple values separated by commas.
[0, 1, 2]: |
|
It seems the input validation is not correct. Select your hobbies:
[0] Playing game
[1] Sleep
[2] Badminton
You can specify multiple values separated by commas.
[0, 1, 2] : ^,2
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ $hobbies │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
array (2) [
0 => string (12) "Playing game"
2 => string (9) "Badminton"
]
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Called from .../app/Controllers/Home.php:13 [dd()]Select your hobbies:
[0] Playing game
[1] Sleep
[2] Badminton
You can specify multiple values separated by commas.
[0, 1, 2] : ,
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ $hobbies │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
array (1) [
0 => string (12) "Playing game"
]
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Called from .../app/Controllers/Home.php:13 [dd()]Select your hobbies:
[0] Playing game
[1] Sleep
[2] Badminton
You can specify multiple values separated by commas.
[0, 1, 2] : |^
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ $hobbies │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
array (1) [
0 => string (12) "Playing game"
]
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Called from .../app/Controllers/Home.php:13 [dd()] |
kenjis
left a comment
There was a problem hiding this comment.
Please fix the input validation.
|
You have two Select your hobbies:
[0] Playing game
[1] Sleep
[2] Badminton
You can specify multiple values separated by commas.
[0, 1, 2] : 1,2,5
Please select correctly.
You can specify multiple values separated by commas.
[0, 1, 2] : 2.5
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ $hobbies │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
array (1) [
2 => string (9) "Badminton"
]
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Called from .../app/Controllers/Home.php:13 [dd()]Select your hobbies:
[0] Playing game
[1] Sleep
[2] Badminton
You can specify multiple values separated by commas.
[0, 1, 2] : 1,2,5
Please select correctly.
You can specify multiple values separated by commas.
[0, 1, 2] : ^
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ $hobbies │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
array (1) [
0 => string (12) "Playing game"
]
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Called from .../app/Controllers/Home.php:13 [dd()] |
- combine two validation to one while loop
|
@rifalarya-2 I put two inline comments. That's all. |
Co-authored-by: kenjis <kenji.uui@gmail.com>
Thank you |
Description
I make this method because method
promptByKey()just can read one value.Checklist: