Skip to content

Commit 98079d4

Browse files
committed
update, some bug fixed
1 parent 018a610 commit 98079d4

File tree

15 files changed

+221
-146
lines changed

15 files changed

+221
-146
lines changed

README_zh.md

Lines changed: 110 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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
248304
public 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
256312
public static function section(string $title, string|array $body, array $opts = [])
257313
```
258314

259-
段落式输出
260-
261-
#### `Show::aList()/$output->aList()`
315+
#### 列表数据展示输出
262316

263317
```php
264318
public 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 = [
275329
Show::aList($data, $title);
276330
```
277331

278-
#### `Show::mList()/$output->mList()` 别名方法 `Show::multiList()`
332+
#### 多列表数据展示输出
279333

280334
```php
281335
public 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 = [
299352
Show::mList($data);
300353
```
301354

302-
#### `Show::panel()/$output->panel()`
355+
#### 面板展示信息输出
303356

304357
```php
305358
public 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
313366
public 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 = [
345398
Show::table($data, 'a table', $opts);
346399
```
347400

348-
#### `Show::helpPanel()/$output->helpPanel()`
401+
#### 快速的渲染一个帮助信息面板
349402

350403
```php
351404
public static function helpPanel(array $config, $showAfterQuit = true)
352405
```
353406

354-
快速的渲染一个帮助信息面板
407+
使用 `Show::helpPanel()/$output->helpPanel()`
355408

356409
```php
357410
Show::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
439490
echo $select; // 'b'
440491
```
441492

442-
### `Interact::confirm()`
493+
### 要求确认是否继续执行
443494

444495
```php
445496
public 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
466515
var_dump($result); // bool(false)
467516
```
468517

469-
### `Interact::question()`/`Interact::ask()`
518+
### 询问,并返回用户的回答
470519

471520
```php
472521
public static function ask($question, $default = null, \Closure $validator = null)
473522
public 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

examples/HomeController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
use inhere\console\utils\Interact;
99

1010
/**
11-
* default command controller. there are some command usage examples
11+
* default command controller. there are some command usage examples(1)
12+
*
13+
* Class HomeController
14+
* @package inhere\console\examples
1215
*/
1316
class HomeController extends Controller
1417
{
15-
const DESCRIPTION = 'default command controller. there are some command usage examples';
18+
const DESCRIPTION = 'default command controller. there are some command usage examples(2)';
1619

1720
/**
1821
* this is a command's description message

examples/TestCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
*/
1818
class TestCommand extends Command
1919
{
20+
/**
21+
* {@inheritdoc}
22+
*/
2023
public function execute($input, $output)
2124
{
22-
$this->output->write('hello, this in ' . __METHOD__);
25+
$output->write('hello, this in ' . __METHOD__);
2326

2427
// $this->output->panel($_SERVER, 'Server information', '');
2528

File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/opt_arg_parse.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/AbstractCommand.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ public function __construct(Input $input, Output $output)
5151

5252
abstract public function run($arg = '');
5353

54+
protected function beforeRun($action)
55+
{
56+
}
57+
58+
protected function afterRun($action)
59+
{
60+
}
61+
62+
/**
63+
* handle action/command runtime exception
64+
*
65+
* @param \Throwable $e
66+
* @throws \Throwable
67+
*/
68+
protected function handleRuntimeException(\Throwable $e)
69+
{
70+
throw $e;
71+
}
72+
5473
/**
5574
* @param string $name
5675
*/
@@ -78,7 +97,7 @@ public static function getAllowTags(): array
7897
/**
7998
* @param array $allowTags
8099
*/
81-
public static function setAllowTags($allowTags)
100+
public static function setAllowTags(array $allowTags)
82101
{
83102
self::$allowTags = $allowTags;
84103
}

src/App.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ public function runAction($name, $action, $believable = false)
136136

137137
/** @var Controller $object */
138138
$object = new $controller($this->input, $this->output);
139-
$object->setName($name);
140139

141140
if (!($object instanceof Controller)) {
142141
throw new \InvalidArgumentException("The console controller class [$object] must instanceof the " . Controller::class);
143142
}
144143

144+
$object->setName($name);
145+
145146
return $object->run($action);
146147
}
147148

0 commit comments

Comments
 (0)