CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Add new method `promptByMultipleKeys()` in CLI class

Open rivalarya opened this issue 3 years ago • 8 comments

Description I make this method because method promptByKey() just can read one value.

Checklist:

  • [x] Securely signed commits
  • [x] Component(s) with PHPDoc blocks, only if necessary or adds value
  • [x] Unit testing, with >80% coverage
  • [x] User guide updated
  • [x] Conforms to style guide

rivalarya avatar Jul 27 '22 09:07 rivalarya

Sorry, this branch has conflicts. Can you rebase and solve the conflicts?

And now we can write test for CLI::input(). See #6335 Could you try to add a test if possible?

kenjis avatar Aug 05 '22 07:08 kenjis

Sorry, this branch has conflicts. Can you rebase and solve the conflicts?

And now we can write test for CLI::input(). See #6335 Could you try to add a test if possible?

okay. i will write the test

rivalarya avatar Aug 05 '22 08:08 rivalarya

promptByKey():

$ 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]: 

promptByMultipleKeys():

$ 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]

kenjis avatar Aug 10 '22 02:08 kenjis

The output format is a bit different from promptByKey().

  1. Can you make the same?
  2. I did not know how to input multiple values.

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]:

kenjis avatar Aug 10 '22 02:08 kenjis

The output format is a bit different from promptByKey().

  1. Can you make the same?
  2. I did not know how to input multiple values.

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]:
  1. So, the output should only return the value of the selected key?
  2. There is an example in the documentation. see here

rivalarya avatar Aug 10 '22 02:08 rivalarya

So, the output should only return the value of the selected key?

I think key value pair is okay.

There is an example in the documentation.

End users don't know whether promptByKey() or promptByMultipleKeys() is used.

kenjis avatar Aug 10 '22 03:08 kenjis

Please select correctly.

You can specify multiple values separated by commas.
[0, 1, 2]:

In promptByKey, when a user fails in validation input, the return is:

The temp field must be one of: 0, 1, 2, 3.

[0, 1, 2, 3]:

But in promptByMultipleKeys, when a user fails in validation input, the return is:

Please select correctly.

Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

[0, 1, 2]:

Is it fine? or should I replace the return in promptByMultipleKeys?

rivalarya avatar Aug 10 '22 03:08 rivalarya

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]:

kenjis avatar Aug 10 '22 04:08 kenjis

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 avatar Aug 14 '22 07:08 kenjis

You have two while (true). It seems they have the different validation rules. They should be combined into one.

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()]

kenjis avatar Aug 18 '22 02:08 kenjis

@rifalarya-2 I put two inline comments. That's all.

kenjis avatar Aug 18 '22 07:08 kenjis

@rifalarya-2 I put two inline comments. That's all.

Thank you

rivalarya avatar Aug 18 '22 08:08 rivalarya