Skip to content

Commit 1e87d14

Browse files
committed
update readme, a bug fixed for Show::helpPanel
1 parent ed8da95 commit 1e87d14

File tree

8 files changed

+103
-48
lines changed

8 files changed

+103
-48
lines changed

README.md

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,32 @@ a php console application library.
99

1010
## usage
1111

12-
```
12+
```php
1313
use inhere\console\io\Input;
1414
use inhere\console\io\Output;
1515
use inhere\console\App;
1616

17+
$config = [];
1718
$input = new Input;
1819
$output = new Output;
19-
$app = new App([], $input, $output);
20+
$app = new App($config, $input, $output);
21+
22+
// add command routes
23+
$app->command('demo', function (Input $in, Output $out) {
24+
$cmd = $in->getCommand();
25+
26+
$out->info('hello, this is a test command: ' . $cmd);
27+
});
28+
29+
// run
30+
$app->run();
2031
```
2132

2233
## input
2334

2435
example(in terminal):
2536

26-
```
37+
```bash
2738
$ examples/app home/useArg status=2 name=john arg0 -s=test --page=23 --id=23 --id=154 -e dev -v vvv -d -rf --debug --test=false
2839
```
2940

@@ -48,15 +59,11 @@ var_dump($input->getArgs());
4859
output:
4960

5061
```php
51-
array(3) {
52-
'status' => string(1) "2"
53-
'name' => array(3) {
54-
[0] => string(4) "john"
55-
[1] => string(3) "tom"
56-
[2] => string(4) "jack"
57-
}
58-
[0] => string(4) "arg0"
59-
}
62+
array(3) {
63+
'status' => string(1) "2"
64+
'name' => string(4) "john"
65+
[0] => string(4) "arg0"
66+
}
6067
```
6168

6269
get parsed options:
@@ -68,26 +75,23 @@ var_dump($input->getOpts());
6875
output:
6976

7077
```php
71-
array(8) {
78+
array(10) {
7279
's' => string(4) "test"
73-
'page' => string(2) "23"
74-
'id' => array(3) {
75-
[0] => string(2) "23"
76-
[1] => string(3) "154"
77-
[2] => string(3) "456"
78-
}
80+
'e' => string(3) "dev"
81+
'v' => string(3) "vvv"
7982
'd' => bool(true)
8083
'r' => bool(true)
8184
'f' => bool(true)
82-
'debug' => bool(true)
83-
'test' => bool(false)
84-
}
85+
'page' => string(2) "23"
86+
'id' => string(3) "154"
87+
'debug' => bool(true)
88+
'test' => bool(false)
89+
}
8590
```
8691

8792
more method:
8893

8994
```php
90-
9195
// argument
9296
$first = $input->getFirstArg(); // 'arg0'
9397
$status = $input->get('status', 'default'); // '2'
@@ -100,7 +104,7 @@ $test = $input->boolOpt('test') // False
100104

101105
### get user input:
102106

103-
```
107+
```php
104108
echo "Your name:";
105109

106110
$text = $input->read();
@@ -122,17 +126,17 @@ $output->write($message);
122126

123127
#### use color style
124128

125-
![alt text](images/output-color-text.jpg "Title")
129+
![alt text](images/output-color-text.png "Title")
126130

127-
#### special format
131+
#### special format output
128132

129133
- `$output->title()`
130134
- `$output->section()`
131135
- `$output->panel()`
132136
- `$output->table()`
133137
- `$output->helpPanel()`
134138

135-
![alt text](images/output-panel-table-title.jpg "Title")
139+
![alt text](images/output-format-msg.png "Title")
136140

137141
## more interactive
138142

@@ -144,7 +148,7 @@ interactive method:
144148

145149
Select one of the options
146150

147-
```
151+
```php
148152
select($description, $options, $default = null, $allowExit=true)
149153
choice($description, $options, $default = null, $allowExit=true)
150154
```
@@ -153,14 +157,15 @@ choice($description, $options, $default = null, $allowExit=true)
153157

154158
only values, no setting option
155159

156-
```
160+
```php
157161
$select = Interact::select('Your city is ?', [
158162
'chengdu', 'beijing', 'shanghai'
159163
]);
160164

161165
```
162166

163167
output in terminal:
168+
164169
```
165170
Your city is ?
166171
0) chengdu
@@ -170,15 +175,15 @@ Your city is ?
170175
You choice: 0
171176
```
172177

173-
```
178+
```php
174179
echo $select; // '0'
175180
```
176181

177182
- example 2:
178183

179184
custom option, setting a default value.
180185

181-
```
186+
```php
182187
$select = Interact::select('Your city is ?', [
183188
'a' => 'chengdu',
184189
'b' => 'beijing',
@@ -197,22 +202,21 @@ Your city is?
197202
You choice[default:a] : b
198203
```
199204

200-
```
205+
```php
201206
echo $select; // 'b'
202207
```
203208

204209
### `Interact::confirm()`
205210

206-
```
207-
confirm(string $question, bool $default = true) bool
211+
```php
212+
public static function confirm($question, $default = true) bool
208213
```
209214

210215
usage:
211216

212217

213-
```
218+
```php
214219
$result = Interact::confirm('Whether you want to continue ?');
215-
216220
```
217221

218222
output in terminal:
@@ -224,19 +228,29 @@ Please confirm (yes|no) [default:yes]: n
224228

225229
result:
226230

227-
```
231+
```php
228232
var_dump($result); // bool(false)
229233
```
230234

231235

232-
### `Interact::question()`
233-
234-
### `Interact::loopAsk()`
236+
### `Interact::question()`/`Interact::ask()`
235237

238+
```php
239+
public static function ask($question, $default = null, \Closure $validator = null)
240+
public static function question($question, $default = null, \Closure $validator = null)
236241
```
237242

238-
```
243+
```php
244+
$answer = Interact::ask('Please input your name?', null, function ($answer) {
245+
if ( !preg_match('/\w+/', $answer) ) {
246+
Interact::error('The name must match "/\w+/"');
239247

248+
return false;
249+
}
250+
251+
return true;
252+
});
253+
```
240254
## License
241255

242256
MIT

examples/HomeController.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use inhere\console\Controller;
66
use inhere\console\utils\Download;
7+
use inhere\console\utils\Show;
78
use inhere\console\utils\Interact;
89

910
/**
@@ -68,14 +69,16 @@ public function blockMsgCommand()
6869
*/
6970
public function fmtMsgCommand()
7071
{
71-
$this->output->title('title');
72+
$this->output->title('title show');
73+
echo "\n";
74+
7275
$body = 'If screen size could not be detected, or the indentation is greater than the screen size, the text will not be wrapped.' .
7376
'Word wrap text with indentation to fit the screen size,' .
7477
'Word wrap text with indentation to fit the screen size,' .
7578
'Word wrap text with indentation to fit the screen size,' .
7679
'Word wrap text with indentation to fit the screen size,';
7780

78-
$this->output->section('section title', $body, [
81+
$this->output->section('section show', $body, [
7982
'pos' => 'l'
8083
]);
8184

@@ -84,8 +87,46 @@ public function fmtMsgCommand()
8487
'help' => 'Show application help information',
8588
'list' => 'List all group and independent commands',
8689
];
87-
Interact::panel($commands, 'Internal Commands', '');
88-
Interact::aList($commands, 'Internal Commands');
90+
Show::panel($commands, 'panel show', '#');
91+
92+
echo "\n";
93+
Show::helpPanel([
94+
Show::HELP_DES => 'a help panel description text. (help panel show)',
95+
Show::HELP_USAGE => 'a usage text',
96+
Show::HELP_ARGUMENTS => [
97+
'arg1' => 'arg1 description',
98+
'arg2' => 'arg2 description',
99+
],
100+
Show::HELP_OPTIONS => [
101+
'--opt1' => 'a long option',
102+
'-s' => 'a short option',
103+
'-d' => 'Run the server on daemon.(default: <comment>false</comment>)',
104+
'-h, --help' => 'Display this help message'
105+
],
106+
], false);
107+
108+
Show::aList($commands, 'aList show');
109+
110+
Show::table([
111+
[
112+
'id' => 1,
113+
'name' => 'john',
114+
'status' => 2,
115+
'email' => '[email protected]',
116+
],
117+
[
118+
'id' => 2,
119+
'name' => 'tom',
120+
'status' => 0,
121+
'email' => '[email protected]',
122+
],
123+
[
124+
'id' => 3,
125+
'name' => 'jack',
126+
'status' => 1,
127+
'email' => '[email protected]',
128+
],
129+
], 'table show');
89130
}
90131

91132
/**
@@ -118,7 +159,7 @@ public function confirmCommand()
118159
}
119160

120161
/**
121-
* use <default>Interact::select</default> method
162+
* use <normal>Interact::select</normal> method
122163
*
123164
*/
124165
public function selectCommand()

images/output-color-text.jpg

-16.1 KB
Binary file not shown.

images/output-color-text.png

190 KB
Loading

images/output-format-msg.png

370 KB
Loading
-36.8 KB
Binary file not shown.

src/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ final protected function showCommandList()
207207
}
208208

209209
$commands[] = "\nFor more information please use: <info>$sName/help [command]</info>";
210-
$this->output->aList($commands, '<comment>Sub-Commands:</comment>');
210+
$this->output->aList($commands, '<comment>Commands:</comment>');
211211
}
212212

213213
/**

src/utils/Show.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public static function helpPanel(array $config, $showAfterQuit = true)
350350

351351
// is key-value [ 'key1' => 'text1', 'key2' => 'text2']
352352
} else {
353-
$value = Helper::spliceKeyValue($config['commands'], [
353+
$value = Helper::spliceKeyValue($value, [
354354
'leftChar' => ' ',
355355
'keyStyle' => 'info',
356356
]);

0 commit comments

Comments
 (0)