|
2 | 2 |
|
3 | 3 | namespace Inhere\Console\Examples\Controllers; |
4 | 4 |
|
5 | | -use Inhere\Console\Controller; |
6 | | -use Inhere\Console\IO\Input; |
7 | 5 | use Inhere\Console\Components\AnsiCode; |
8 | 6 | use Inhere\Console\Components\Download; |
| 7 | +use Inhere\Console\Controller; |
| 8 | +use Inhere\Console\IO\Input; |
9 | 9 | use Inhere\Console\Utils\Helper; |
10 | 10 | use Inhere\Console\Utils\Interact; |
11 | 11 | use Inhere\Console\Utils\Show; |
@@ -37,17 +37,6 @@ public function indexCommand() |
37 | 37 | $this->write('hello, welcome!! this is ' . __METHOD__); |
38 | 38 | } |
39 | 39 |
|
40 | | - /** |
41 | | - * a example for input password on command line |
42 | | - * @usage {fullCommand} |
43 | | - */ |
44 | | - public function passwdCommand() |
45 | | - { |
46 | | - $pwd = $this->askPassword(); |
47 | | - |
48 | | - $this->write('Your input is:' . $pwd); |
49 | | - } |
50 | | - |
51 | 40 | /** |
52 | 41 | * a example for use color text output on command |
53 | 42 | * @usage {fullCommand} |
@@ -392,18 +381,74 @@ public function confirmCommand() |
392 | 381 | { |
393 | 382 | $a = Interact::confirm('continue'); |
394 | 383 |
|
395 | | - $this->write('you answer is: ' . ($a ? 'yes' : 'no')); |
| 384 | + $this->write('Your answer is: ' . ($a ? 'yes' : 'no')); |
396 | 385 | } |
397 | 386 |
|
398 | 387 | /** |
399 | | - * This is a demo for use <magenta>Interact::select</magenta> method |
| 388 | + * This is a demo for use <magenta>Interact::select()</magenta> method |
400 | 389 | */ |
401 | 390 | public function selectCommand() |
402 | 391 | { |
403 | 392 | $opts = ['john', 'simon', 'rose']; |
404 | 393 | $a = Interact::select('you name is', $opts); |
405 | 394 |
|
406 | | - $this->write('you answer is: ' . $opts[$a]); |
| 395 | + $this->write('Your answer is: ' . $opts[$a]); |
| 396 | + } |
| 397 | + |
| 398 | + /** |
| 399 | + * This is a demo for use <magenta>Interact::ask()</magenta> method |
| 400 | + */ |
| 401 | + public function askCommand() |
| 402 | + { |
| 403 | + $a = Interact::ask('you name is: ', null, function ($val, &$err) { |
| 404 | + if (!preg_match('/^\w{2,}$/', $val)) { |
| 405 | + $err = 'Your input must match /^\w{2,}$/'; |
| 406 | + |
| 407 | + return false; |
| 408 | + } |
| 409 | + |
| 410 | + return true; |
| 411 | + }); |
| 412 | + |
| 413 | + $this->write('Your answer is: ' . $a); |
| 414 | + } |
| 415 | + |
| 416 | + /** |
| 417 | + * This is a demo for use <magenta>Interact::limitedAsk()</magenta> method |
| 418 | + * @options |
| 419 | + * --nv Not use validator. |
| 420 | + * --limit limit times.(default: 3) |
| 421 | + */ |
| 422 | + public function limitedAskCommand() |
| 423 | + { |
| 424 | + $times = (int)$this->input->getOpt('limit', 3); |
| 425 | + |
| 426 | + if ($this->input->getBoolOpt('nv')) { |
| 427 | + $a = Interact::limitedAsk('you name is: ', null, null, $times); |
| 428 | + } else { |
| 429 | + $a = Interact::limitedAsk('you name is: ', null, function ($val) { |
| 430 | + if (!preg_match('/^\w{2,}$/', $val)) { |
| 431 | + Show::error('Your input must match /^\w{2,}$/'); |
| 432 | + |
| 433 | + return false; |
| 434 | + } |
| 435 | + |
| 436 | + return true; |
| 437 | + }, $times); |
| 438 | + } |
| 439 | + |
| 440 | + $this->write('Your answer is: ' . $a); |
| 441 | + } |
| 442 | + |
| 443 | + /** |
| 444 | + * a example for input password on command line. use: <magenta>Interact::askPassword()</magenta> |
| 445 | + * @usage {fullCommand} |
| 446 | + */ |
| 447 | + public function pwdCommand() |
| 448 | + { |
| 449 | + $pwd = $this->askPassword(); |
| 450 | + |
| 451 | + $this->write('Your input is: ' . $pwd); |
407 | 452 | } |
408 | 453 |
|
409 | 454 | /** |
|
0 commit comments