@@ -86,9 +86,63 @@ more please see: examples/app [controller|command]
8686
8787```
8888
89+ ## 添加命令
90+
91+ 添加命令的方式有三种
92+
93+ - 如上所示,使用闭包可以快速的添加一个简单的命令
94+ - 通过继承 ` inhere\console\Command ` 添加独立命令
95+
96+ ``` php
97+ use inhere\console\utils\AnsiCode;
98+
99+ /**
100+ * Class Test
101+ * @package app\console\commands
102+ */
103+ class TestCommand extends Command
104+ {
105+ /**
106+ * {@inheritdoc}
107+ */
108+ public function execute($input, $output)
109+ {
110+ $output->write('hello, this in ' . __METHOD__);
111+ }
112+ }
113+ ```
114+
115+ - 通过继承 ` inhere\console\Controller ` 添加一组命令(命令行的控制器类)
116+
117+ ``` php
118+ use inhere\console\Controller;
119+
120+ /**
121+ * default command controller. there are some command usage examples
122+ */
123+ class HomeController extends Controller
124+ {
125+ const DESCRIPTION = 'default command controller. there are some command usage examples';
126+
127+ /**
128+ * this is a command's description message
129+ * the second line text
130+ * @usage usage message
131+ * @example example text one
132+ * the second line example
133+ */
134+ public function indexCommand()
135+ {
136+ $this->write('hello, welcome!! this is ' . __METHOD__);
137+ }
138+ }
139+ ```
140+
141+ 更多请查看 [ ] ( ./examples ) 中的示例代码
142+
89143## 输入
90144
91- > 输出对象是 ` inhere\console\io\Input ` 的实例
145+ > 输入对象是 ` inhere\console\io\Input ` 的实例
92146
93147在终端中执行如下命令,用于演示参数选项等信息的解析:
94148
@@ -242,29 +296,29 @@ $output->write('hello <info>world<info>');
242296
243297> output 实例拥有 ` inhere\console\utils\Show ` 的所有格式化输出方法。不过都是通过对象式访问的。
244298
245- #### ` Show::title()/$output->title() `
299+ #### 标题文本输出
300+
301+ 使用 ` Show::title()/$output->title() `
246302
247303``` php
248304public static function title(string $title, array $opts = [])
249305```
250306
251- 标题文本输出
307+ #### 段落式文本输出
252308
253- #### ` Show::section()/$output->section() `
309+ 使用 ` Show::section()/$output->section() `
254310
255311``` php
256312public static function section(string $title, string|array $body, array $opts = [])
257313```
258314
259- 段落式输出
260-
261- #### ` Show::aList()/$output->aList() `
315+ #### 列表数据展示输出
262316
263317``` php
264318public static function aList(array $data, string $title, array $opts = [])
265319```
266320
267- 列表数据展示输出
321+ 使用 ` Show::aList()/$output->aList() `
268322
269323``` php
270324$title = 'list title';
@@ -275,13 +329,12 @@ $data = [
275329Show::aList($data, $title);
276330```
277331
278- #### ` Show::mList()/$output->mList() ` 别名方法 ` Show::multiList() `
332+ #### 多列表数据展示输出
279333
280334``` php
281335public static function multiList(array $data, array $opts = [])
282336```
283-
284- 多列表数据展示输出
337+ 使用 ` Show::mList()/$output->mList() ` 别名方法 ` Show::multiList() `
285338
286339``` php
287340$data = [
@@ -299,21 +352,21 @@ $data = [
299352Show::mList($data);
300353```
301354
302- #### ` Show::panel()/$output->panel() `
355+ #### 面板展示信息输出
303356
304357``` php
305358public static function panel(mixed $data, $title = 'Information Panel', $borderChar = '*')
306359```
360+ 使用 ` Show::panel()/$output->panel() `
307361
308- 面板展示信息输出
309362
310- #### ` Show::table()/$output->table() `
363+ #### 数据表格信息输出
311364
312365``` php
313366public static function table(array $data, $title = 'Data Table', array $opts = [])
314367```
315368
316- 数据表格信息输出
369+ 使用 ` Show::table()/$output->table() `
317370
318371- 可直接渲染从数据库拉取的数据(会自动提取字段名作为表头)
319372
@@ -345,13 +398,13 @@ $opts = [
345398Show::table($data, 'a table', $opts);
346399```
347400
348- #### ` Show::helpPanel()/$output->helpPanel() `
401+ #### 快速的渲染一个帮助信息面板
349402
350403``` php
351404public static function helpPanel(array $config, $showAfterQuit = true)
352405```
353406
354- 快速的渲染一个帮助信息面板
407+ 使用 ` Show::helpPanel()/$output->helpPanel() `
355408
356409``` php
357410Show::helpPanel([
@@ -378,17 +431,15 @@ Show::helpPanel([
378431
379432需引入类 ` inhere\console\utils\Interact `
380433
381- interactive method:
382-
383- ### ` Interact::select() ` (alias ` Interact::chioce() ` )
384-
385- 从给出的列表中选择
434+ ### 从给出的列表中选择一项
386435
387436``` php
388- select($description, $options, $default = null, $allowExit=true)
389- choice($description, $options, $default = null, $allowExit=true)
437+ public static function select($description, $options, $default = null, $allowExit=true)
438+ public static function choice($description, $options, $default = null, $allowExit=true) // alias method
390439```
391440
441+ 使用 ` Interact::select() ` (alias ` Interact::chioce() ` )
442+
392443- 示例 1: 只有值,没有选项key
393444
394445``` php
@@ -439,15 +490,13 @@ You choice[default:a] : b
439490echo $select; // 'b'
440491```
441492
442- ### ` Interact::confirm() `
493+ ### 要求确认是否继续执行
443494
444495``` php
445496public static function confirm($question, $default = true) bool
446497```
447498
448- 要求确认是否继续执行
449-
450- 使用:
499+ 使用 ` Interact::confirm() ` :
451500
452501``` php
453502$result = Interact::confirm('Whether you want to continue ?');
@@ -466,25 +515,50 @@ Please confirm (yes|no) [default:yes]: n
466515var_dump($result); // bool(false)
467516```
468517
469- ### ` Interact::question() ` / ` Interact::ask() `
518+ ### 询问,并返回用户的回答
470519
471520``` php
472521public static function ask($question, $default = null, \Closure $validator = null)
473522public static function question($question, $default = null, \Closure $validator = null)
474523```
475524
476- 询问,并返回用户的回答
525+ 使用 ` Interact::question() ` / ` Interact::ask() `
477526
478527``` php
479- $answer = Interact::ask('Please input your name?', null, function ($answer) {
480- if ( !preg_match('/\w+/', $answer) ) {
528+ $answer = Interact::ask('Please input your name?', null, function ($answer) {
529+ if (!preg_match('/\w+/', $answer)) {
481530 Interact::error('The name must match "/\w+/"');
482-
531+
483532 return false;
484- }
533+ }
534+
535+ return true;
536+ });
537+ ```
538+
539+ ### 有次数限制的询问
485540
486- return true;
487- });
541+ ``` php
542+ public static function limitedAsk($question, $default = null, \Closure $validator = null, $times = 3)
543+ ```
544+
545+ 有次数限制的询问,提出问题
546+
547+ * 若输入了值且验证成功则返回 输入的结果
548+ * 否则,会连续询问 ` $times ` 次,若仍然错误,退出
549+
550+
551+ ``` php
552+ // no default value
553+ $answer = Interact::limitedAsk('please input you age?', null, function($age)
554+ {
555+ if ($age<1 || $age >100) {
556+ Interact::error('Allow the input range is 1-100');
557+ return false;
558+ }
559+
560+ return true;
561+ });
488562```
489563
490564## License
0 commit comments