-
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
Add new method promptByMultipleKeys()
in CLI class
#6302
Add new method promptByMultipleKeys()
in CLI class
#6302
Conversation
- 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
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is confusing.
- The PHPDoc says
$text
is astring
. - The method parameter says it is untyped.
- The conditional says
$text
must be astring|array
- The throwable says
$text
can 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we cannot write test for it. See #6076
It is a problem.
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.
Hmm. This is the problem with using static methods. It is not mockable by PHPUnit.
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.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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]: |
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.
See my comments.
|
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()] |
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.
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 <[email protected]>
Thank you |
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.
LGTM! Thank you.
Description
I make this method because method
promptByKey()
just can read one value.Checklist: